iOS Question decrease line spacing for label and textfield

Keith Yong

Active Member
Licensed User
Longtime User
Is there any properties to set the line height/spacing for label and textfield?
Screen Shot 2016-06-29 at 6.26.59 PM.png
 

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,
try this:
B4X:
Sub SetLineSpace(TF As TextField,LineSpace As Float)
    Dim A As AttributedString
    A.Initialize(TF.Text,TF.Font,TF.TextColor)
  
    Dim NaObj2 As NativeObject
    NaObj2 = NaObj2.Initialize("NSMutableParagraphStyle").RunMethod("new",Null)
    NaObj2.RunMethod("setLineSpacing:",Array(LineSpace))
    NaObj2.RunMethod("setAlignment:",Array(TF.TextAlignment))
  
    Dim NaObj As NativeObject
    NaObj = NaObj.Initialize("NSMutableAttributedString").RunMethod("alloc",Null).RunMethod("initWithAttributedString:",Array(A))
    NaObj.RunMethod("addAttribute:value:range:",Array("NSParagraphStyle",NaObj2,NaObj.MakeRange(0,TF.Text.Length)))
  
    Dim NaObj3 As NativeObject = TF
    NaObj3.SetField("attributedText",NaObj)
End Sub

Call this sub after you have all properties set to your TextField. It should also work for labels

Jan
 
Last edited:
Upvote 0

Keith Yong

Active Member
Licensed User
Longtime User
Hi,
try this:
B4X:
Sub SetLineSpace(TF As TextField,LineSpace As Float)
    Dim A As AttributedString
    A.Initialize(TF.Text,TF.Font,TF.TextColor)
 
    Dim NaObj2 As NativeObject
    NaObj2 = NaObj2.Initialize("NSMutableParagraphStyle").RunMethod("new",Null)
    NaObj2.RunMethod("setLineSpacing:",Array(LineSpace))
    NaObj2.RunMethod("setAlignment:",Array(TF.TextAlignment))
 
    Dim NaObj As NativeObject
    NaObj = NaObj.Initialize("NSMutableAttributedString").RunMethod("alloc",Null).RunMethod("initWithAttributedString:",Array(A))
    NaObj.RunMethod("addAttribute:value:range:",Array("NSParagraphStyle",NaObj2,NaObj.MakeRange(0,TF.Text.Length)))
 
    Dim NaObj3 As NativeObject = TF
    NaObj3.SetField("attributedText",NaObj)
End Sub

Call this sub after you have all properties set to your TextField. It should also work for labels

Jan

I have tried this but it just shows 1 line "Mobile" only, and I set the linespace as 1.
 
Upvote 0

Keith Yong

Active Member
Licensed User
Longtime User
lblObject.Initialize("")
lblObject.TextAlignment=0
lblObject.TextColor = Color.White
lblDisplay.Text="Mobile number"
lblDisplay.Multiline=True
lblDisplay.Width=displayWidth
lblDisplay.SizeToFit
SetLabelLineSpace(lblDisplay,1)

this is how I set the value
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Ok, I think LineSpace was the wrong route.
Try this instead (you can set LineSpace to 0):
B4X:
Sub SetLineProperties(TF As Label,LineSpace As Float,MinimumLineHeight As Float, MaximumLineHeight As Float)
    Dim A As AttributedString
    A.Initialize(TF.Text,TF.Font,TF.TextColor)
 
    Dim NaObj2 As NativeObject
    NaObj2 = NaObj2.Initialize("NSMutableParagraphStyle").RunMethod("new",Null)
    NaObj2.RunMethod("setLineSpacing:",Array(LineSpace))
    NaObj2.RunMethod("setAlignment:",Array(TF.TextAlignment))
      NaObj2.RunMethod("setMinimumLineHeight:",Array(MinimumLineHeight))
      NaObj2.RunMethod("setMaximumLineHeight:",Array(MaximumLineHeight))
   
    Dim NaObj As NativeObject
    NaObj = NaObj.Initialize("NSMutableAttributedString").RunMethod("alloc",Null).RunMethod("initWithAttributedString:",Array(A))
    NaObj.RunMethod("addAttribute:value:range:",Array("NSParagraphStyle",NaObj2,NaObj.MakeRange(0,TF.Text.Length)))
 
    Dim NaObj3 As NativeObject = TF
    NaObj3.SetField("attributedText",NaObj)
End Sub

Jan
 
Upvote 0

Keith Yong

Active Member
Licensed User
Longtime User
Ok, I think LineSpace was the wrong route.
Try this instead (you can set LineSpace to 0):
B4X:
Sub SetLineProperties(TF As Label,LineSpace As Float,MinimumLineHeight As Float, MaximumLineHeight As Float)
    Dim A As AttributedString
    A.Initialize(TF.Text,TF.Font,TF.TextColor)

    Dim NaObj2 As NativeObject
    NaObj2 = NaObj2.Initialize("NSMutableParagraphStyle").RunMethod("new",Null)
    NaObj2.RunMethod("setLineSpacing:",Array(LineSpace))
    NaObj2.RunMethod("setAlignment:",Array(TF.TextAlignment))
      NaObj2.RunMethod("setMinimumLineHeight:",Array(MinimumLineHeight))
      NaObj2.RunMethod("setMaximumLineHeight:",Array(MaximumLineHeight))
  
    Dim NaObj As NativeObject
    NaObj = NaObj.Initialize("NSMutableAttributedString").RunMethod("alloc",Null).RunMethod("initWithAttributedString:",Array(A))
    NaObj.RunMethod("addAttribute:value:range:",Array("NSParagraphStyle",NaObj2,NaObj.MakeRange(0,TF.Text.Length)))

    Dim NaObj3 As NativeObject = TF
    NaObj3.SetField("attributedText",NaObj)
End Sub

Jan


Yes! it works perfectly!!
 
Upvote 0
Top