如何没有“扩展”的textview?

问题描述 投票:0回答:2

我遇到一个小问题,即我的标签文本已展开,如下图所示:

Click on this link

我想知道如何获得正常的文字。

如果您知道该怎么做,请告诉我。

这是代码:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.


    let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.updateUserActivityOnApp(isFirstConnection: false)

    //print(self.view.frame.size.height - (self.extraPhoto1.frame.size.height + self.extraPhoto1.frame.origin.y))

    if self.view.frame.size.height - (self.extraPhoto1.frame.size.height + self.extraPhoto1.frame.origin.y) >= 140 {

        descriptionTopSpaceConstraint.constant = 20

        self.view.layoutIfNeeded()

    }

    profilePicture.image = nil

    descriptionTxtView.isEditable = false

    descriptionTxtView.text! = ""

    extraPhoto1.image = nil

    extraPhoto2.image = nil

    extraPhoto3.image = nil 

    usernameCountryLbl.text! = "Loading..."

    self.navigationItem.setHidesBackButton(true, animated:true)

    var attrStr = NSMutableAttributedString(string: "this is some very long test string for justification. this is some very long test string for justification. this is some very long test string for justification. this is some very long test string for justification")

    var paragraphStyle = NSMutableParagraphStyle()

    paragraphStyle.alignment = NSTextAlignment.justified
    paragraphStyle.hyphenationFactor = 1
    attrStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attrStr.length))
    attrStr.addAttribute(NSAttributedString.Key.kern, value: -0.1, range: NSMakeRange(0, attrStr.length))
    self.descriptionTxtView.attributedText = attrStr // label is the UILabel where you want the text to be displayed
    self.descriptionTxtView.sizeToFit()

}

非常感谢你

ios swift
2个回答
1
投票

textAligment.natural作为@Rob提到的是一个选项,以防你不关心你的文本是否合理。否则唯一的方法是使用NSMutableAttributedStringhyphenationFactor。如果您需要证明它是合理的,您可以这样做:

    var attrStr = NSMutableAttributedString(string: "this is some very long test string for justification. this is some very long test string for justification. this is some very long test string for justification. this is some very long test string for justification")
    var paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = NSTextAlignment.justified
    paragraphStyle.hyphenationFactor = 1
    attrStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attrStr.length))
    attrStr.addAttribute(NSAttributedString.Key.kern, value: -0.1, range: NSMakeRange(0, attrStr.length))
    label.attributedText = attrStr // label is the UILabel where you want the text to be displayed
    label.sizeToFit()
    label.numberOfLines = 0

1
投票

将文本视图的textAlignment设置为.natural而不是.justified

© www.soinside.com 2019 - 2024. All rights reserved.