나는 UITextView
의 줄 수를 찾아야 해. UITextView
에 numberOfLines
처럼 사용할 수있는 속성이 없습니다. 다음 수식을 사용하지만 작동하지 않습니다. 아무도 이것에 대해 생각을 갖고 있습니까? 당신이 아이폰 OS 3을 사용하는 경우UITextView 찾는 방법
int numLines = txtview.contentSize.height/txtview.font.lineHeight;
나는 UITextView
의 줄 수를 찾아야 해. UITextView
에 numberOfLines
처럼 사용할 수있는 속성이 없습니다. 다음 수식을 사용하지만 작동하지 않습니다. 아무도 이것에 대해 생각을 갖고 있습니까? 당신이 아이폰 OS 3을 사용하는 경우UITextView 찾는 방법
int numLines = txtview.contentSize.height/txtview.font.lineHeight;
, 당신은 leading
속성을 사용할 필요가 :
int numLines = txtview.contentSize.height/txtview.font.lineHeight;
: 당신이 아이폰 OS 4를 사용하는 경우
int numLines = txtview.contentSize.height/txtview.font.leading;
을, 당신은 lineHeight
속성을 사용합니다
그리고, @thomas가 지적했듯이 정확한 결과가 필요하면 반올림에주의하십시오.
당신은에 (총있는 UIScrollView의 텍스트 라인의 수를 얻을 수있는 UITextView의 글꼴의 줄 간격으로 픽셀 단위 텍스트의 높이 및 나누기를 얻으려면 UITextView의 contentSize 속성을 볼 수 있으며, 꺼짐 화면). 줄 바꿈 텍스트와 줄 바꿈 텍스트가 모두 포함됩니다. UITextInputTokenizer
를 사용 UITextView
의 행 수를 계산
int numLines = txtview.contentSize.height/txtview.font.leading;
스위프트 4 방법 :
또한public extension UITextView {
/// number of lines based on entered text
public var numberOfLines: Int {
guard compare(beginningOfDocument, to: endOfDocument).same == false else {
return 0
}
let direction: UITextDirection = UITextStorageDirection.forward.rawValue
var lineBeginning = beginningOfDocument
var lines = 0
while true {
lines += 1
guard let lineEnd = tokenizer.position(from: lineBeginning, toBoundary: .line, inDirection: direction) else {
fatalError()
}
guard compare(lineEnd, to: endOfDocument).same == false else {
break
}
guard let newLineBeginning = tokenizer.position(from: lineEnd, toBoundary: .character, inDirection: direction) else {
fatalError()
}
guard compare(newLineBeginning, to: endOfDocument).same == false else {
return lines + 1
}
lineBeginning = newLineBeginning
}
return lines
}
}
public extension ComparisonResult {
public var ascending: Bool {
switch self {
case .orderedAscending:
return true
default:
return false
}
}
public var descending: Bool {
switch self {
case .orderedDescending:
return true
default:
return false
}
}
public var same: Bool {
switch self {
case .orderedSame:
return true
default:
return false
}
}
}
'Value of type'을 컴파일하지 않습니다. 'ComparisonResult'에는 'same'멤버가 없습니다. 일부 개인 확장? – Pahnev
: 수식 (INT 주조 하부로 끝나는 된 부동 소수점 값을 생성 경계). 아마도 결과를 반올림하면 더 나은 결과를 얻을 수 있습니다 :'int numLines = round (...) '그러면 0.9999의 결과는 1이되고 0이 아닙니다. – thomas
@thomas : True. 나는 이것을 답에 추가했다. –