我正在尝试制作带有删除线的属性字符串。我可以设置其他属性,例如前景色和字体大小,但是当我尝试在部分文本中设置删除线时,该部分文本消失了。知道是什么原因造成的吗?
下面是代码。感谢您的关注!
// ...
//Price
NSLog(@"RESULT NUMBER %d", cell.result.resultId);
priceString = (priceString == nil) ? @"$150.00\n$100.00" : priceString;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:priceString];
NSRange linebreak = [priceString rangeOfString:@"\n"];
if (linebreak.location != NSNotFound) {
[attributedString beginEditing];
// RegPrice
NSRange firstLine = NSMakeRange(0, linebreak.location);
// [attributedString addAttribute:NSFontAttributeName
// value:[UIFont boldSystemFontOfSize:11]
// range:firstLine];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1]
range:firstLine];
@try {
[attributedString addAttribute:NSStrikethroughStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:firstLine];
} @catch (NSException *e) {
NSLog(@"ATTRIBUTE EXCEPTION: %@", e);
}
// Sale Price
[attributedString addAttribute:NSFontAttributeName …Run Code Online (Sandbox Code Playgroud) 这就是我想要实现的目标:
var range : NSRange? = NSMakeRange(0, (attributedString?.length)!)
attributedString?.attribute(NSFontAttributeName: UIFont.fontNames(forFamilyName: "OpenSans-Regular"), at: 1, effectiveRange: &range)
Run Code Online (Sandbox Code Playgroud)
在第二行,我收到一个编译时错误:
参数标签 '(NSFontAttributeName:, at:, EffectiveRange:)' 不匹配任何可用的重载
如果 UIButton 同时设置了标题和属性标题,则显示的是属性标题。
如果我将按钮设置为属性标题:
myButton.setAttributedTitle(myAttribText, for: .normal)
Run Code Online (Sandbox Code Playgroud)
稍后在应用程序中,我想将按钮设置为常规标题:
myButton.setTitle(myRegularText, for: .normal)
Run Code Online (Sandbox Code Playgroud)
是否有一行代码可以用来删除按钮上的属性标题,以便它不会覆盖我为按钮设置的新标题?谢谢!
我试图熟悉自己CoreText,并开始制作自己的笔记应用程序。
我将正则表达式与我现在正在输入的文本框结合使用AttributedStrings,并试图从本质上模仿它的功能。StackOverflow
我有所有常见的东西,例如:
大胆的
斜体
emphasis blocks
但我正在努力制作块引用/代码块。
像这样的东西,它会中断自己的行并创建一个框,无论文本有多长,该框都会到达边缘。
And it changes the background color
Run Code Online (Sandbox Code Playgroud)
严格使用 AttributedStrings 是否可以做到这一点?我见过一些注入 HTML / CSS 的旧示例,但我希望我可以使用一些特殊的NSAttributedStringKey组合来完成它。
我想获取 UILabel 中某些文本的长度,并动态更改 UILabel 的 width 属性。
但是我不知道如何在函数boundingrect中设置参数。这是 Apple Developer 的文档。
func boundingRect(with size: CGSize,
options: NSStringDrawingOptions = [],
context: NSStringDrawingContext?) -> CGRect
Run Code Online (Sandbox Code Playgroud)
,我试过这样
let attr = NSMutableAttributedString(string: "test test test test , this is test text. test test test test , this is test text. test test test test , this is test text. ")
//attr.addattributes
print(attr.boundingrect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: 0), option: .truncatesLastVisibleLine
,context: nil)!)
Run Code Online (Sandbox Code Playgroud)
但最后我在打印时得到了错误的宽度,那么为什么以及如何获得正确的宽度。
我有 NSAttributedStringKey 和 UITextView 的问题
事实上,我正在尝试这样做:
这是理论上应该工作的代码
class ViewController: UIViewController,UITextViewDelegate {
@IBOutlet weak var txtView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
txtView.delegate = self;
let linkAttributes : [NSAttributedStringKey : Any] = [
.link: URL(string: "https://exemple.com")!,
.font:UIFont.boldSystemFont(ofSize: 18),
.foregroundColor: UIColor.blue] ;
let attributedString = NSMutableAttributedString(string: "Just
click here to do stuff...")
attributedString.setAttributes(linkAttributes, range:
NSMakeRange(5, 10))
txtView.attributedText = attributedString;
}}
Run Code Online (Sandbox Code Playgroud)
但此代码显示此
所以我试图通过添加两行代码来解决这个问题:
let linkAttributes : [NSAttributedStringKey : Any] = [
.link: URL(string: "https://exemple.com")!,
.font:UIFont.boldSystemFont(ofSize: 18),
.foregroundColor: UIColor.blue,
.strokeColor : UIColor.black,//new line
.strokeWidth …Run Code Online (Sandbox Code Playgroud) 我正在开发一个简单的应用程序并以编程方式添加NSAttributedString. 我真的很困惑如何将礼物viewcontroller转移到另一个viewcontroller。
这不是重复的问题,因为我的问题是如何移动到视图控制器和重复问题如何在单击时打开链接 如何在 NSAttributedString 中创建可点击的链接?.
截屏

在上图中显示了三个,NSAttributedString第一个是dhiman.sham1gmail.com,第二个是has started following,第三个Sham。尝试单击时Sham不执行任何操作。我想在单击时将此控制器移动到另一个控制器Sham。
这是我的代码:
@IBOutlet weak var descriptionLbl: UILabel!
override func updateWithModel(_ model: AnyObject) {
self.descriptionLbl.attributedText = self.followingGetTextFromType(data: (model as? FollowingNotificationData)!)
}
func followingGetTextFromType(data:FollowingNotificationData) -> NSMutableAttributedString{
var attributedString = NSMutableAttributedString()
attributedString = NSMutableAttributedString(string:(data.detailsNoti?.fullName)!, attributes: strres)
let startedfollowing = NSMutableAttributedString(string:" has started following ", attributes: attrs)
attributedString.append(startedfollowing)
var discription = NSMutableAttributedString()
if data.fullName == ""{ …Run Code Online (Sandbox Code Playgroud) uiviewcontroller nsattributedstring ios swift nsattributedstringkey
我使用UITextView启用了文本属性编辑的 ,当文本中有表情符号时,我在获取属性时遇到问题。
这是我使用的代码:
var textAttributes = [(attributes: [NSAttributedString.Key: Any], range: NSRange)]()
let range = NSRange(location: 0, length: textView.attributedText.length)
textView.attributedText.enumerateAttributes(in: range) { dict, range, _ in
textAttributes.append((attributes: dict, range: range))
}
for attribute in textAttributes {
if let swiftRange = Range(attribute.range, in: textView.text) {
print("NSRange \(attribute.range): \(textView.text![swiftRange])")
} else {
print("NSRange \(attribute.range): cannot convert to Swift range")
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用“示例文本 ??”之类的文本时,输出如下:
NSRange {0, 12}:示例文本
NSRange {12, 1}:无法转换为 Swift 范围
NSRange {13, 1}:无法转换为 Swift 范围
如您所见,我无法获取带有表情符号的文本。
文本属性由我NSTextStorage在文本视图上应用的自定义设置。这是setAttributes …
当我将图像附件添加到带有前景色设置的 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
Run Code Online (Sandbox Code Playgroud)
当我删除 时.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
Run Code Online (Sandbox Code Playgroud)
这是在 Xcode 11.0、iOS 13 上开发的。这是 UITextView/iOS …
我有一个 UITextField,我将当前值设置为属性文本,如下所示:
public var currentValue: NSAttributedString {
get {
return inputField.attributedText ?? NSAttributedString()
}
set {
inputField.attributedText = newValue
}
}
Run Code Online (Sandbox Code Playgroud)
这以前只是一个字符串,但我现在需要为部分文本添加格式。
但是,我有一种方法需要传递这些字段,该方法以查看该字段是否为空的条件开头。以前我是这样开始的:
if textField.currentValue.isEmpty {
// Perform logic
}
Run Code Online (Sandbox Code Playgroud)
然而,显然 NSAttributedText 没有 isEmpty 成员。如何对 NSAttributedText 执行相同的检查?