如何在UITextView swift4中将AttributedText链接居中

时间:2018-08-03 23:04:08

标签: ios swift4

有什么办法可以使文本居中并更改大小/字体?

我将其设置在情节提要中,但运行该应用程序后没有任何反应。我也尝试使用UILabel,但是它没有用(当我单击它时,网络没有显示)

我发现他们的答案并不只是URL attributes.paragraphstyle

我不知道怎么写,所以我可以保留URL字符串,同时更改字体/大小/对齐方式。

func searchMealRecipe() {
    MealRecipeService.sharedInstance.foodID = foodID
    MealRecipeService.sharedInstance.getMealRecipe { (responseString, mealRecipeItem) in
        if responseString == "error" {
             /* popup here alerting user about error*/ 
        } else {
             self.mealRecipeItem = mealRecipeItem
             self.ingredientRecipeTitleLabel.text = (mealRecipeItem.name)?.htmlString?.string
             self.ingredientRecipeImageView.sd_setImage(with: URL(string: mealRecipeItem.imageURL!), placeholderImage: UIImage(named: ""))
             self.ingredientRecipeTextView.text = mealRecipeItem.ingredients?.joined(separator: "\n")
             let string = "Sounds Good! Show Me the Steps "
             let attributedString = NSMutableAttributedString(string: string, attributes:[NSAttributedString.Key.link: URL(string: "\(mealRecipeItem.link!)")!])
             self.ingredientRecipeLinkTextView.attributedText = attributedString
        }
     }
}

enter image description here

我正在使用xcode10 beta和swift4.2

//Update(I figured it out the answer and it's working)
//the attributes accept array so I can put multiple styles in the array. 

let string = "Sounds Good! Show Me the Steps "
let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.center
let attributedString = NSMutableAttributedString(string: string, attributes:[NSAttributedString.Key.link: URL(string: "\(mealRecipeItem.link!)")!,  NSAttributedString.Key.paragraphStyle: style])
self.ingredientRecipeLinkTextView.attributedText = attributedString

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用NSMutableParagraphStyle

let style = NSMutableParagraphStyle()

 style.alignment = NSTextAlignment.center

 lbl.centerAttributedText = NSAttributedString(string: "Total Balance",attributes: [your Attributed URL Code here])