当我将图像附件添加到具有前景色设置的UITextView时,图像将被设置为空白:
let attrString = NSMutableAttributedString(string: rawText, attributes: [.font: UIFont.systemFont(ofSize: 17), .foregroundColor: UIColor.black])
let attachment = NSTextAttachment(image: image)
let imgStr = NSMutableAttributedString(attachment: attachment)
attrString.append(imgStr)
textview.attributedText = attrString
当我删除.foregroundColor: UIColor.black
时,图像可以正确显示,但是我需要能够设置属性的文字颜色。
在添加图片附件后,我尝试显式删除.foregroundColor
属性,但是没有运气。我还尝试从大多数文本中删除.foregroundColor
属性,但仍然无法正常工作,仅从整个字符串中删除该属性即可:
attrString.removeAttribute(.foregroundColor, range: NSRange(location: attrString.length-1, length: 1)) // does not work
// -------
attrString.removeAttribute(.foregroundColor, range: NSRange(location: 1, length: attrString.length-1)) // does not work
// -------
attrString.removeAttribute(.foregroundColor, range: NSRange(location: 0, length: attrString.length)) // works but no text colour
这是在iOS 13的Xcode 11.0上开发的。这是UITextView / iOS错误还是预期的行为(我认为不太可能)?如何使用设置的文字颜色正确显示图像?
答案 0 :(得分:1)
namespace GeoProcessing.Google
{
public class GeoProcessing : IGeoProcessing
{
const string GoogleMapsKey = "XXYY";
public async Task<ICoordinates> Coordinates(string address)
{
. . .
string url = $"https://maps.googleapis.com/maps/api/geocode/json?address={city}, {region}&key={GoogleMapsKey}";
构造函数似乎存在错误(在iOS 13上,在回答此问题时),以下图像附件构造正常工作:
NSTextAttachment(image:)