У меня есть строка, которую я хочу установить в UILabel или UITextView, и у меня нет межстрочного интервала или очень мало.Я знаю, что стиль NSParagraph имеет max и min lineHeight, а также lineHeightMultiple.Я перепробовал все и до сих пор получаю странные результаты.Возможно, также необходимо использовать атрибут смещения базовой линии, но если это так, как найти динамическое значение, чтобы интервал между линиями был ровным и очень небольшим между строками?
Вот пример.Чтобы сохранить работу, у меня есть переключатель, чтобы каждая строка могла использовать свой собственный стиль абзаца.
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
label.numberOfLines = 0
label.backgroundColor = UIColor.green
view.addSubview(label)
self.view = view
let tryWithIndParagraphStyles = false
if tryWithIndParagraphStyles{
let mutAttr = NSMutableAttributedString()
let para = NSMutableParagraphStyle()
para.alignment = .center
//WE COULD TRY LINE HEIGHT OR LINE HEIGHT MULTIPLE but the spacing is still wrong
// para.lineHeightMultiple = 0.75
//we could manually set the line heights for each
let firstFont = UIFont.systemFont(ofSize: 75.56262594898386)
let firstPara = NSMutableParagraphStyle()
firstPara.alignment = .center
firstPara.minimumLineHeight = firstFont.pointSize - firstFont.ascender + firstFont.capHeight
firstPara.maximumLineHeight = firstFont.pointSize - firstFont.ascender + firstFont.capHeight
let firstStr = NSAttributedString(string: "HEY\r", attributes: [.font:firstFont,.paragraphStyle:firstPara])
mutAttr.append(firstStr)
let secondFont = UIFont.systemFont(ofSize: 24.099621199347652)
let secondPara = NSMutableParagraphStyle()
secondPara.alignment = .center
secondPara.minimumLineHeight = secondFont.pointSize - secondFont.ascender + secondFont.capHeight
secondPara.maximumLineHeight = secondFont.pointSize - secondFont.ascender + secondFont.capHeight
let secondStr = NSAttributedString(string: "BEGGING TO\r", attributes: [.font:secondFont,.paragraphStyle:secondPara])
mutAttr.append(secondStr)
let thirdFont = UIFont.systemFont(ofSize: 59.53419014162365)
let thirdPara = NSMutableParagraphStyle()
thirdPara.alignment = .center
thirdPara.minimumLineHeight = thirdFont.pointSize - thirdFont.ascender + thirdFont.capHeight
thirdPara.maximumLineHeight = thirdFont.pointSize - thirdFont.ascender + thirdFont.capHeight
let thirdStr = NSAttributedString(string: "TEST\r", attributes: [.font:thirdFont,.paragraphStyle:thirdPara])
mutAttr.append(thirdStr)
let fourthFont = UIFont.systemFont(ofSize: 20.96469640066495)
let fourthPara = NSMutableParagraphStyle()
fourthPara.alignment = .center
fourthPara.minimumLineHeight = fourthFont.pointSize - fourthFont.ascender + fourthFont.capHeight
thirdPara.maximumLineHeight = fourthFont.pointSize - fourthFont.ascender + fourthFont.capHeight
let fourthStr = NSAttributedString(string: "HAMBURGERS", attributes: [.font:fourthFont,.paragraphStyle:fourthPara])
mutAttr.append(fourthStr)
label.attributedText = mutAttr
label.sizeToFit()
}else{
let mutAttr = NSMutableAttributedString()
let para = NSMutableParagraphStyle()
para.alignment = .center
//WE COULD TRY LINE HEIGHT OR LINE HEIGHT MULTIPLE but the spacing is still wrong
para.lineHeightMultiple = 0.75
//we could manually set the line heights for each
let firstFont = UIFont.systemFont(ofSize: 75.56262594898386)
let firstStr = NSAttributedString(string: "HEY\r", attributes: [.font:firstFont,.paragraphStyle:para])
mutAttr.append(firstStr)
let secondStr = NSAttributedString(string: "BEGGING TO\r", attributes: [.font:UIFont.systemFont(ofSize: 24.099621199347652),.paragraphStyle:para])
mutAttr.append(secondStr)
let thirdStr = NSAttributedString(string: "TEST\r", attributes: [.font:UIFont.systemFont(ofSize: 59.53419014162365),.paragraphStyle:para])
mutAttr.append(thirdStr)
let fourthStr = NSAttributedString(string: "HAMBURGERS", attributes: [.font:UIFont.systemFont(ofSize: 20.96469640066495),.paragraphStyle:para])
mutAttr.append(fourthStr)
label.attributedText = mutAttr
label.sizeToFit()
}
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()