Saya memiliki label yang memiliki beberapa baris teks dan saya ingin menambah jarak antar baris. Ada pertanyaan serupa yang diajukan oleh orang lain tetapi solusinya tidak menyelesaikan masalah saya. Juga label saya mungkin atau mungkin tidak berisi paragraf. Saya baru mengenal Swift
. Apakah ada solusi menggunakan storyboard? Atau hanya melalui NSAttributedString
kemungkinannya?
ios
swift
uilabel
line-spacing
Sneha
sumber
sumber
NSMutableAttributedString
sebagai gantinyaNSAttributedString
. Saya telah memperbarui asnwer.NSMutableAttributedString
. Bisa menggunakanNSAttributedString(string: "Your text", attributes: [NSAttributedString.Key.paragraphStyle : paragraphStyle])
Dari Pembuat Antarmuka:
Secara terprogram:
SWift 4 & 4.2
Menggunakan ekstensi label
extension UILabel { func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) { guard let labelText = self.text else { return } let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = lineSpacing paragraphStyle.lineHeightMultiple = lineHeightMultiple let attributedString:NSMutableAttributedString if let labelattributedText = self.attributedText { attributedString = NSMutableAttributedString(attributedString: labelattributedText) } else { attributedString = NSMutableAttributedString(string: labelText) } // (Swift 4.2 and above) Line spacing attribute attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length)) // (Swift 4.1 and 4.0) Line spacing attribute attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length)) self.attributedText = attributedString } }
Sekarang fungsi ekstensi panggilan
let label = UILabel() let stringValue = "Set\nUILabel\nline\nspacing" // Pass value for any one argument - lineSpacing or lineHeightMultiple label.setLineSpacing(lineSpacing: 2.0) . // try values 1.0 to 5.0 // or try lineHeightMultiple //label.setLineSpacing(lineHeightMultiple = 2.0) // try values 0.5 to 2.0
Atau menggunakan contoh label (Cukup salin & jalankan kode ini untuk melihat hasilnya)
let label = UILabel() let stringValue = "Set\nUILabel\nline\nspacing" let attrString = NSMutableAttributedString(string: stringValue) var style = NSMutableParagraphStyle() style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48 style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40 // Line spacing attribute attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: stringValue.characters.count)) // Character spacing attribute attrString.addAttribute(NSAttributedStringKey.kern, value: 2, range: NSMakeRange(0, attrString.length)) label.attributedText = attrString
Cepat 3
let label = UILabel() let stringValue = "Set\nUILabel\nline\nspacing" let attrString = NSMutableAttributedString(string: stringValue) var style = NSMutableParagraphStyle() style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48 style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40 attrString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: stringValue.characters.count)) label.attributedText = attrString
sumber
Anda dapat mengontrol spasi dalam
storyboard
.Pertanyaan yang sama.
sumber
ascender
properti seperti yang disebutkan di sini .Solusi terbaru untuk Swift 5.0
private extension UILabel { // MARK: - spacingValue is spacing that you need func addInterlineSpacing(spacingValue: CGFloat = 2) { // MARK: - Check if there's any text guard let textString = text else { return } // MARK: - Create "NSMutableAttributedString" with your text let attributedString = NSMutableAttributedString(string: textString) // MARK: - Create instance of "NSMutableParagraphStyle" let paragraphStyle = NSMutableParagraphStyle() // MARK: - Actually adding spacing we need to ParagraphStyle paragraphStyle.lineSpacing = spacingValue // MARK: - Adding ParagraphStyle to your attributed String attributedString.addAttribute( .paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length )) // MARK: - Assign string that you've modified to current attributed Text attributedText = attributedString } }
Dan penggunaannya:
let yourLabel = UILabel() let yourText = "Hello \n world \n !" yourLabel.text = yourText yourLabel.addInterlineSpacing(spacingValue: 1.5)
sumber
UILabel.text
dan tidakUILabel.attributedText
Anda dapat menggunakan ekstensi yang dapat digunakan kembali ini:
extension String { func lineSpaced(_ spacing: CGFloat) -> NSAttributedString { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = spacing let attributedString = NSAttributedString(string: self, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle]) return attributedString } }
sumber
Swift 4 dan Swift 5
extension NSAttributedString { func withLineSpacing(_ spacing: CGFloat) -> NSAttributedString { let attributedString = NSMutableAttributedString(attributedString: self) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineBreakMode = .byTruncatingTail paragraphStyle.lineSpacing = spacing attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: string.count)) return NSAttributedString(attributedString: attributedString) } }
Cara Penggunaan
let example = NSAttributedString(string: "This is Line 1 \nLine 2 \nLine 3 ").withLineSpacing(15) testLabel.attributedText = example
sumber
Jawaban Dipen diperbarui untuk Swift 4
let attr = NSMutableAttributedString(string: today) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = 2 attr.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attr.length)) label.attributedText = attr;
sumber
//Swift 4: func set(text:String, inLabel:UILabel, withLineSpacing:CGFloat, alignment:NSTextAlignment){ let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = withLineSpacing let attrString = NSMutableAttributedString(string: text) attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length)) inLabel.attributedText = attrString inLabel.textAlignment = alignment }
sumber