标签: nsattributedstring

标签中属性文本的固有大小不正确

我对标签中的属性文本有疑问,该文本占用了底部的大量空间。当我使用未归属的尺寸时,尺寸还可以,但现在我总是有底部空间 - 在图像上标记为粉红色。我尝试在 Reveal.app 中更改高度约束的值(由 iOS 生成),当我将其设置为比实际小一点时,文本就会被截断。

\n\n

我需要属性标签,因为我正在从 HTML 字符串创建属性:

\n\n
<p style=\\"font-family: EncodeSans-Thin; font-size: 10.0; color: #1f2e3b;\\">\n    Od 2004 roku wspieramy organizacje biznesowe w rozwoju sztuki zarz\xc4\x85dzania projektami. \n    Nasza dzia\xc5\x82alno\xc5\x9b\xc4\x87 opiera si\xc4\x99 na trzech filarach \xe2\x80\x93 \n    <span style=\\"font-family: EncodeSans-Light\\">\n        doradztwie, realizacji projekt\xc3\xb3w na zlecenie oraz szkoleniach.\n    </span>\n</p>\n
Run Code Online (Sandbox Code Playgroud)\n\n

底部空间图

\n\n

你遇到过这样的问题吗?

\n\n

更新:

\n\n

我正在使用下面的函数从 HTML 字符串(作为字符串扩展)生成 NSAttributedString。

\n\n
func makeAttributesFromHTML() -> NSAttributedString {\n    guard let htmlData = self.dataUsingEncoding(NSUTF8StringEncoding) else {\n        return NSAttributedString()\n    }\n\n    do …
Run Code Online (Sandbox Code Playgroud)

html nsattributedstring uilabel ios autolayout

5
推荐指数
1
解决办法
3358
查看次数

自定义 UILabel 中 NSLinkAttributeName 的颜色

我想NSLinkAttributeName在 UILabel 中自定义颜色。但设置NSForegroundColorAttributeName不影响链接文本颜色,它仍然是蓝色。

但NSUnderlineColorAttributeName有效,我能够自定义下划线颜色。是否可以以某种方式更改链接文本颜色?

nsattributedstring ios swift

5
推荐指数
1
解决办法
2012
查看次数

如何使用 NSAttributedString 添加两个单词之间的间距?

假设我已经渲染了一个句子That looks like this。

如果我希望在其中任何一个上有更多的空间like,我该怎么做?所以它实际上看起来像That looks like this.

注入空格是行不通的。

objective-c uitextview nsattributedstring uilabel ios

5
推荐指数
1
解决办法
6356
查看次数

当 UITextVIew 编辑为 True 时检测 NSAttributedString 中附加图像的点击

我正在使用下面的方法来检测UITextView.

`func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool`
Run Code Online (Sandbox Code Playgroud)

该方法仅在 时调用textView.isEditable = false。

所以然后我添加UITapGestureRecognizer,UITextView当用户点击里面的图像时它会调用UITextView。但这一点我不知道如何检测用户点击的图像(如果里面有多个图像)UITextView。我还获取了UITextView水龙头的 x 和 y 位置,但不知道如何获取文本或者它是否是来自这些点的图像

let TapGesture = UITapGestureRecognizer(target: self, action: #selector(tapDetected(sender:)))
TapGesture.delegate = self
textView.addGestureRecognizer(TapGesture)`
Run Code Online (Sandbox Code Playgroud)

我还尝试在 中添加视图textView.addSubview。但我也不知道如果用户想在此子视图之前或之后键入文本,我该如何更改其位置,就像它的行为与NSAttributedString Images相应文本更改其位置相同。

let imgRect : UIBezierPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 30, height: 30))
textView.textContainer.exclusionPaths = [imgRect]
let spacerView : UIView = UIView.init(frame: CGRect(x: …
Run Code Online (Sandbox Code Playgroud)

uitextview nsattributedstring nstextattachment ios swift

5
推荐指数
1
解决办法
4827
查看次数

UITextView 中两个具有不同颜色的超链接?

我正在尝试创建一个 UITextView,其中有两个链接作为文本,但颜色不同。我不确定这是否可能。

我已将 UITextView 设置为检测链接,不可编辑,而只能选择。

到目前为止我所做的如下:

NSMutableAttributedString *termsAndConditionsTitle = [[NSMutableAttributedString alloc] initWithString:@"I have read the " attributes:@{NSLinkAttributeName: @"https://apple.com", NSForegroundColorAttributeName: [UIColor greenColor]}];
NSMutableAttributedString *someString2 = [[NSMutableAttributedString alloc] initWithString:@"terms and conditions" attributes:@{NSLinkAttributeName: @"https://microsoft.com", NSForegroundColorAttributeName: [UIColor redColor]}];
[termsAndConditionsTitle appendAttributedString:someString2];
[self.termsAndConditionsView setAttributedText:termsAndConditionsTitle];
Run Code Online (Sandbox Code Playgroud)

但最终链接只有 UITextView 的色调颜色。是否可以使两个链接具有不同的颜色?提前致谢!

PS 如果可能的话,我不想使用库/框架。

objective-c hyperlink uitextview nsattributedstring ios

5
推荐指数
2
解决办法
993
查看次数

NSAttributedString 文本始终以较大的 lineHeight 粘在底部

我正在尝试实现来自 Sketch 的按设计标签,例如我需要字体大小 = 19 且行高 = 50 的文本样式。所以我最终使用了NSAttributedStringwith NSMutableParagraphStyle,但由于文本粘在底部的问题而停止了UILabel

我已经尝试使用lineHeightMultipleandlineSpacing但那些并没有给我我想要的行高,所以我最终使用minimumLineHeightandmaximumLineHeight等于相同

这是我的方法NSAttributedString

    private static func makeAttributedString(
        with attributes: TextAttributes,
        text: String? = nil,
        alignment: NSTextAlignment = .center
    ) -> NSAttributedString {
        let font = UIFont(name: attributes.font.rawValue, size: attributes.fontSize)!

        let paragraph = NSMutableParagraphStyle()
        paragraph.alignment = alignment
        paragraph.paragraphSpacing = attributes.paragraph
        paragraph.minimumLineHeight = attributes.lineHeight // equal 50 in my case
        paragraph.maximumLineHeight = attributes.lineHeight // equal 50 in my case

        let …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring uilabel ios swift

5
推荐指数
1
解决办法
1567
查看次数

如何在 rtf 文件中写入 NSAttributedString?

如何在 rtf 文件中写入 NSAttributedString?我找到了一个古老的答案如何将属性字符串(文本)保存到文件(swift,cocoa)中? 我不太明白我到底需要什么,也许某个地方存在新的不同方式?

macos rtf nsattributedstring ios swift

5
推荐指数
1
解决办法
1291
查看次数

字符串内的内联按钮和/或文本字段,其包装与使用 Swift 的文本相同

这更多的是如何实现这一目标的问题,而不是具体的解决方案。我想要一个标签,允许按钮和文本字段内联并换行到下一行。一个示例是 iOS 快捷方式应用程序,您可以在其中键入文本所在的同一块,并且文本字段会沿同一行换行。它是一个文本字段,因为有一个插入符号。

iOS 快捷方式应用程序 编辑时的 iOS 快捷方式应用程序

从高层次来看,它可能看起来像这样:

在此输入图像描述

其中每个视图都遵循该线并在需要时换行到下一个视图。

我最初考虑使用带有样式链接的 NSAttributedString,该链接可以充当按钮,也可以打开文本字段进行输入。我尝试了这个并得到了一些有效的东西,但它与 iOS 快捷方式应用程序不同,其中文本字段位于文本中。我也考虑过使用 textkit,但我还没有做到这一点,因为我不确定这是正确的方法。

感谢您提供任何想法或解决方案。

编辑

我想过另一种方法来实现这一目标,但我不知道它是否有效。这个想法是使用文本字段的集合视图。某些文本字段将共享相同的 LayoutManager,以便文本在文本字段之间共享。我必须计算要创建多少个文本字段,以便它们沿着 collectionView 流动

在此输入图像描述

在图像中,Label 1由 1 个禁用编辑的文本字段组成。TextField 1由 3 个启用编辑的文本字段组成,并且这三个文本字段将共享相同的 LayoutManager。Label 2由 2 个带有共享布局管理器的文本字段组成,但禁用编辑。

使用这种方法意味着计算为每个“块”(标签或文本字段)创建多少个文本字段,并在每次内容更改时更新它。通过这种方法,我只考虑标签和文本字段,但可以在其他时间添加按钮。

我刚刚开始这样做,但意识到共享布局管理器会禁用编辑,所以我不知道这是否可以再实现。

谢谢

uitextfield nsattributedstring ios textkit swift

5
推荐指数
1
解决办法
394
查看次数

如何在文本 UILabel 周围实现粗描边边框

这就是我想要实现的目标:

在此输入图像描述

这是我尝试过的一些事情:

NS属性字符串

我尝试使用NSAttributedString以下属性,但 iOS 14+ 中的文本路径似乎存在错误

private var attributes: [NSAttributedString.Key: Any] {
    [
        .strokeColor : strokeColor,
        .strokeWidth : -8,
        .foregroundColor : foregroundColor,
        .font: UIFont.systemFont(ofSize: 17, weight: .black)
    ]
}
Run Code Online (Sandbox Code Playgroud)

NS属性字符串

NSAttributedString如果某些字母没有出现奇怪的路径问题,我对这个结果非常满意。

绘制文本覆盖

我也尝试过覆盖drawText,但据我所知,我找不到任何方法来改变笔划粗细并将其与下一个字符混合在一起:

override func drawText(in rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()
    context?.setLineJoin(.round)
    
    context?.setTextDrawingMode(.stroke)
    self.textColor = strokeColor
    super.drawText(in: rect)
    
    context?.setTextDrawingMode(.fill)
    self.textColor = foregroundColor
    super.drawText(in: rect)
    
    layer.shadowColor = UIColor.black.cgColor
    layer.shadowRadius = 2
    layer.shadowOpacity = 0.08
    layer.shadowOffset = .init(width: 0, height: 2)
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

core-graphics uikit nsattributedstring uilabel swift

5
推荐指数
1
解决办法
1273
查看次数

iPhone中的NSForegroundColorAttributeName键

我听说在iPhone中添加了NSAttributedString但是当我使用键NSForegroundColorAttributeName来设置颜色时,我得到一个错误,说"NSForegroundColorAttributeName undeclared".我错过了什么吗?请帮我.

问候

Yallappa

iphone nsattributedstring uicolor

4
推荐指数
2
解决办法
6297
查看次数