iOS Question UITextView numberOfLines useable from ObjC

Alexander Stolte

Expert
Licensed User
Hello,

is this useable in B4I?
With this is it possible to count the lines of a textview. I need the line Count, because i set the view height per code if the linecount has changed.

My try was this, but i dont know what is wrong:
B4X:
Sub LineCount(view As Object) As Int
    Dim NativeMe As NativeObject = Me
Return    NativeMe.RunMethod("numberOfLines:", Array (view))
End Sub

#If ObjC

extension UITextView {
    func numberOfLines() -> Int {
        let layoutManager = self.layoutManager
        let numberOfGlyphs = layoutManager.numberOfGlyphs
        var lineRange: NSRange = NSMakeRange(0, 1)
        var index = 0
        var numberOfLines = 0

        while index < numberOfGlyphs {
            layoutManager.lineFragmentRectForGlyphAtIndex(
                index, effectiveRange: &lineRange
            )
            index = NSMaxRange(lineRange)
            numberOfLines += 1
        }
        return numberOfLines
    }
}


#End If
 

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,
you are trying to use Swift code, which is not possible with B4i at the moment.
I need the line Count
Try:
B4X:
Sub GetNumberOfLines (TV As TextView) As Int
    If TV.Text.Length = 0 Then Return 1
    Dim No As NativeObject = Me
    #if objc
        -(int)CountLines:(UITextView*)TV{
            return ((TV.contentSize.height / TV.font.lineHeight) -1);
        }
    #End If
    Return No.RunMethod("CountLines:",Array(TV)).AsNumber
End Sub

Jan
 
Upvote 0
Top