簡體   English   中英

使用 NSAttributedString 將 UILabel 中的文本居中

[英]centering text in a UILabel with an NSAttributedString

對我正在處理的應用程序進行一些基本改進。 仍然是 iOS swift 開發場景的新手。 我認為代碼中的文本行會自動居中,因為我將標簽設置為居中。 經過一些研究,我發現情況並非如此。 我將如何將這樣的代碼對齊到中心:

let atrString = try NSAttributedString(
   data: assetDetails!.cardDescription.dataUsingEncoding(NSUTF8StringEncoding)!, 
   options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], 
   documentAttributes: nil)
assetDescription.attributedText = atrString

您需要創建一個指定居中對齊的段落樣式,並將該段落樣式設置為文本的一個屬性。 示例游樂場:

import UIKit
import PlaygroundSupport

let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.center

let richText = NSMutableAttributedString(string: "Going through some basic improvements to a application I am working on. Still new to the iOS swift development scene. I figured that the lines of text in my code would automatically be centered because I set the label to center.",
                                         attributes: [ NSParagraphStyleAttributeName: style ])
// In Swift 4, use `.paragraphStyle` instead of `NSParagraphStyleAttributeName`.

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 400))
label.backgroundColor = UIColor.white
label.attributedText = richText
label.numberOfLines = 0
PlaygroundPage.current.liveView = label

結果:

標簽中的居中文本

由於您正在解析 HTML 文檔以創建屬性字符串,因此您需要在創建后添加屬性,如下所示:

let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.center

let richText = try NSMutableAttributedString(
    data: assetDetails!.cardDescription.data(using: String.Encoding.utf8)!,
    options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],
    documentAttributes: nil)
richText.addAttributes([ NSParagraphStyleAttributeName: style ],
                       range: NSMakeRange(0, richText.length))
// In Swift 4, use `.paragraphStyle` instead of `NSParagraphStyleAttributeName`.
assetDescription.attributedText = richText

Swift 4 更新

在 Swift 4 中,屬性名稱現在是NSAttributeStringKey類型,標准屬性名稱是該類型的靜態成員。 所以你可以像這樣添加屬性:

richText.addAttribute(.paragraphStyle, value: style, range: NSMakeRange(0, richText.length))

在 Swift 4.1 中:

let style = NSMutableParagraphStyle()

style.alignment = NSTextAlignment.center

lbl.centerAttributedText = NSAttributedString(string: "Total Balance",attributes: [.paragraphStyle: style])

(為代碼塊編輯)

您可以使用此實用程序功能對標簽進行所有常見配置


    @discardableResult
    public func DULabel(text: String, frame: CGRect = .zero, parent:UIView? = nil , font:UIFont, textColor:UIColor = .black, numOfLines:Int = 0 ,textAlignment: NSTextAlignment = .center,lineSpaceing:CGFloat = 0, cb: ((UILabel)->Void)? = nil  )-> UILabel! {
      let label = UILabel()
      label.frame = frame
      label.font = font
      label.textColor = textColor
      label.textAlignment = textAlignment
      label.numberOfLines = numOfLines
      if( lineSpaceing == 0 ){
        label.text = text
      }
      else {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpaceing
        paragraphStyle.alignment = textAlignment
        let attrString = NSMutableAttributedString(string: text)
        attrString.addAttribute(.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
        label.attributedText = attrString
      }
      if let parent = parent {
        parent.addSubview(label)
      }
      cb?(label)
      return label
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM