Как увеличить межстрочный интервал в UILabel в Swift 4
Я хочу увеличить межстрочный интервал в UILabel, но не могу понять, как.
Я нашел это решение [ /questions/5580169/kak-uvelichit-mezhstrochnyij-interval-v-uilabel-v-swift/5580181#5580181 в Stackru, но мой Xcode всегда отображает это:
Use of unresolved identifier 'NSParagraphStyleAttributeName'
Я думаю, что ответ правильный, но он не сработал для меня. Кто-нибудь может помочь с этой проблемой?
1 ответ
Решение
В Swift 4 в этом случае вы должны использовать NSAttributedStringKey.paragraphStyle
вместо NSParagraphStyleAttributeName
Swift 5
import UIKit
extension UILabel {
func setLineHeight(lineHeight: CGFloat) {
let text = self.text
if let text = text {
let attributeString = NSMutableAttributedString(string: text)
let style = NSMutableParagraphStyle()
style.lineSpacing = lineHeight
attributeString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSMakeRange(0, attributeString.length))
self.attributedText = attributeString
}
}
}
Это сработало для меня.
import UIKit
extension UILabel {
func setLineHeight(lineHeight: CGFloat) {
let text = self.text
if let text = text {
let attributeString = NSMutableAttributedString(string: text)
let style = NSMutableParagraphStyle()
style.lineSpacing = lineHeight
attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(0, count(text)))
self.attributedText = attributeString
}
}
}
Проверьте это смехотворно простое решение, которое работает!
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CommentTVCCell
// Configure the cell...
cell.myLabel?.text = "\n[\(indexPath.row + 1)]\n\n" + itemArray[indexPath.row].name
//
return cell
}
// Обратите внимание на использование \n для загрузки возврата или столько, сколько вы хотите. Убедитесь, что они находятся в строковом обозначении.