简体   繁体   English

字体大小重置为NSAttributedString

[英]Font Size resets in NSAttributedString

Am adding an image to a NSAttributedString object so I can display them properly in a UITextView . 我正在将图像添加到NSAttributedString对象,以便可以在UITextView正确显示它们。 So far, it works great. 到目前为止,效果很好。 Only problem is, my Font size is 25 to match my image size but each time I add an image to the UITextView , the Font resets to some smaller size. 唯一的问题是,我的字体大小为25以匹配我的图像大小,但是每次将图像添加到UITextView ,字体都会重置为较小的大小。 I need to keep the font at 25 to match texts with the image size. 我需要将字体保持在25,以使文本与图像大小匹配。

NSAttributedString has 3 constructors which take string , attributedString and string:attributes: . NSAttributedString具有3个构造函数,这些构造函数采用stringattributedStringstring:attributes: NSAttributedString

Since I'm not using string, I can't set the attributes using the third constructor and the first 2 won't allow me to set attributes ( UIFont ). 由于我没有使用字符串,因此无法使用第三个构造函数来设置属性,而前两个构造函数将不允许我设置属性( UIFont )。 I therefore decided to set the Font size from Interface Builder and reset it each time I add an image to the `UITextField. 因此,我决定从Interface Builder设置Font大小,并在每次将图像添加到UITextField时重置它。

Am facing a problem here because each time I reset the font, the UITextView scrolls to the first line (assuming there is so much text, it has scrolled up). 这里遇到了一个问题,因为每次我重置字体时, UITextView滚动到第一行(假设有很多文本,它已经向上滚动)。 How do I set font size after adding image without the UITextView scrolling up automatically? 在添加图像后如何在UITextView不自动向上滚动的情况下设置字体大小? Better still, is there a better way of doing this? 更好的是,还有更好的方法吗? Thanks. 谢谢。

NSMutableAttributed字符串可以尝试一些方法,包括在构造属性字符串之后跨范围设置属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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