我对标签中的属性文本有疑问,该文本占用了底部的大量空间。当我使用未归属的尺寸时,尺寸还可以,但现在我总是有底部空间 - 在图像上标记为粉红色。我尝试在 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>\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n你遇到过这样的问题吗?
\n\n更新:
\n\n我正在使用下面的函数从 HTML 字符串(作为字符串扩展)生成 NSAttributedString。
\n\nfunc makeAttributesFromHTML() -> NSAttributedString {\n guard let htmlData = self.dataUsingEncoding(NSUTF8StringEncoding) else {\n return NSAttributedString()\n }\n\n do …Run Code Online (Sandbox Code Playgroud) 我想NSLinkAttributeName在 UILabel 中自定义颜色。但设置NSForegroundColorAttributeName不影响链接文本颜色,它仍然是蓝色。
但NSUnderlineColorAttributeName有效,我能够自定义下划线颜色。是否可以以某种方式更改链接文本颜色?
假设我已经渲染了一个句子That looks like this。
如果我希望在其中任何一个上有更多的空间like,我该怎么做?所以它实际上看起来像That looks like this.
注入空格是行不通的。
我正在使用下面的方法来检测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,其中有两个链接作为文本,但颜色不同。我不确定这是否可能。
我已将 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 如果可能的话,我不想使用库/框架。
我正在尝试实现来自 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) 如何在 rtf 文件中写入 NSAttributedString?我找到了一个古老的答案如何将属性字符串(文本)保存到文件(swift,cocoa)中? 我不太明白我到底需要什么,也许某个地方存在新的不同方式?
这更多的是如何实现这一目标的问题,而不是具体的解决方案。我想要一个标签,允许按钮和文本字段内联并换行到下一行。一个示例是 iOS 快捷方式应用程序,您可以在其中键入文本所在的同一块,并且文本字段会沿同一行换行。它是一个文本字段,因为有一个插入符号。
从高层次来看,它可能看起来像这样:
其中每个视图都遵循该线并在需要时换行到下一个视图。
我最初考虑使用带有样式链接的 NSAttributedString,该链接可以充当按钮,也可以打开文本字段进行输入。我尝试了这个并得到了一些有效的东西,但它与 iOS 快捷方式应用程序不同,其中文本字段位于文本中。我也考虑过使用 textkit,但我还没有做到这一点,因为我不确定这是正确的方法。
感谢您提供任何想法或解决方案。
我想过另一种方法来实现这一目标,但我不知道它是否有效。这个想法是使用文本字段的集合视图。某些文本字段将共享相同的 LayoutManager,以便文本在文本字段之间共享。我必须计算要创建多少个文本字段,以便它们沿着 collectionView 流动
在图像中,Label 1由 1 个禁用编辑的文本字段组成。TextField 1由 3 个启用编辑的文本字段组成,并且这三个文本字段将共享相同的 LayoutManager。Label 2由 2 个带有共享布局管理器的文本字段组成,但禁用编辑。
使用这种方法意味着计算为每个“块”(标签或文本字段)创建多少个文本字段,并在每次内容更改时更新它。通过这种方法,我只考虑标签和文本字段,但可以在其他时间添加按钮。
我刚刚开始这样做,但意识到共享布局管理器会禁用编辑,所以我不知道这是否可以再实现。
谢谢
这就是我想要实现的目标:
这是我尝试过的一些事情:
我尝试使用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)
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)
我听说在iPhone中添加了NSAttributedString但是当我使用键NSForegroundColorAttributeName来设置颜色时,我得到一个错误,说"NSForegroundColorAttributeName undeclared".我错过了什么吗?请帮我.
问候
Yallappa
ios ×8
swift ×6
uilabel ×4
uitextview ×3
objective-c ×2
autolayout ×1
html ×1
hyperlink ×1
iphone ×1
macos ×1
rtf ×1
textkit ×1
uicolor ×1
uikit ×1
uitextfield ×1