简体   繁体   中英

Insert line break for UILabel text when a specific character is found

I have a UILabel that dynamically gets updated every time I click a button, the data will be fetched from firebase and displayed on the uilabel.I am able to display the text as shown below. I would like to insert a line break when a specific delimiter(say '.' PERIOD) is encountered in the text. I have looked into many solutions about UIlabel line break but couldn't find one what exactly I am looking for, every solution deals with static text that we provide in the storyboard itself. Any help will be appreciated. Thank you. 在此处输入图片说明 )

UILabel文字内容

Created an outlet for the label and have taken a string which has three sentences separated by ".". The content in the image attached will be this string. Try using replacingOccurences() function as given below.

@IBOutlet weak var trialLabel: UILabel!

var string = "This is a trial text.Let us jump to a new line when the full stop is encountered.Hope it helps."

string = string.replacingOccurrences(of: ".", with: ".\n")
trialLabel.text = string

Check https://developer.apple.com/documentation/foundation/nsstring/1412937-replacingoccurrences for further reference. Happy Coding!!

You can achieve this with the attributed text property of UILabel. Try to find and replace the character with html line break and then assign this text to the UILabel attributed text.

You can replace string by

let str = "This is the string to be replaced by a new string" let replacedStr = str.replacingOccurrences(of: "string", with: "str")

use below code // HTML Tag Remove Method

extension String{

    func removeHtmlFromString(inPutString: String) -> String{

        return inPutString.replacingOccurrences(of: ".", with: ".\n", options: .regularExpression, range: nil)
    }
}


   let str = "Lorem Ipsum is simply dummy text.Lorem Ipsum has been the industry's standard dummy text ever since the 1500."

  let postDiscription = str!.removeHtmlFromString(inPutString: str!)

您可以在文本字符串中添加\\ n到要创建换行符的位置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM