- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为我的 NSMutableAttributedString
添加一个特定范围的背景属性,只要起始范围为 0,它就可以正常工作。每当我尝试将索引传递给函数时背景被添加到比预期更多的文本中。
这是演示该问题的视频。每当用户写错字母时,问题就会出现。
代码:
var index = 0
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
guard let char = string.cString(using: String.Encoding.utf8) else { return true }
let isBackSpace = strcmp(char, "\\b")
if (isBackSpace == -92)
{
addBackground(color: .clear, from: index - 1, to: index)
index -= 1
}
else
{
if charactersDoesMatch(char: string, i: index)
{
addBackground(color: .green, from: 0, to: index + 1)
}
else
{
//As you can see im trying to add it from the current index to the next one only.
addBackground(color: .red, from: index, to: index + 1)
}
index += 1
}
return true
}
private func addBackground(color: UIColor, from: Int, to: Int)
{
attributedText?.addAttribute(NSAttributedStringKey.backgroundColor, value: color, range: NSMakeRange(from, to))
toTypelabel.attributedText = attributedText
}
最佳答案
我想我解决了你的问题
func NSMakeRange(_ loc: Int,
_ len: Int) -> NSRange
接受参数位置,第二个是长度,所以当输入错误的值时,您输入 1 作为参数,这意味着只有一个字符应为红色
addBackground(color: .red, from: index, to: 1)
更多阅读苹果文档: NSMakeRange
关于ios - 在特定范围内向 NSMutableAttributedString 添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52495554/
我想将一个 NSMutableAttributedString 中的所有属性复制到一个新的中。我试过的代码是这样的: [attrStr enumerateAttribute:NSFontAttribu
我正在尝试更改应在用户界面中显示的标签的字体。我用过这行: let myAttribute = [ NSFontAttributeName: UIFont(name: "cirth1", size:
我使用 UIlabel 创建了一个 xib 文件。我想为 UILabel 添加行距。我编写了下面的代码,但它不会改变行之间的行间距。 (我尝试使用属性编辑器,但没有解决)有什么问题吗? 该代码适用于其
我有一个带有如下所示字符串的标签。我希望 SecondVariable 具有不同的颜色。我想我明白如何改变颜色。我的问题是获取 secondVariable 的范围。 let str = "\(fir
我制作了一个 NSMutableAttributedString 对象,它由 2 个不同的 NSMutableAttributedString 对象组成: NSMutableAttributedStr
let text = "By accepting you agree to our Terms of Service and Privacy Policy." let attr = NSMutabl
帮助! 崩溃发生在: [emtionString replaceCharactersInRange:range withString:imageAndRangeDicArray[i][@"image"
我在下面有一个方法,我想用它来更改 UILabel 文本的最后 6 个字符的颜色,这将是括号中的日期,即 (1999 )。首先,我设置了 tableViewCell 的文本,然后获取了 attribu
我在设置此 NSMutableAttributedString 时对范围做了一些错误(我认为)。谁能告诉我为什么这会导致我的程序崩溃?在我看来是对的,但我显然错了!谢谢。 NSString *plac
我正在做类似 facebook 的标记功能,我已经可以标记用户了。我可以这样展示。它是由这段代码完成的。 NSMutableAttributedString * string = [[NSMutabl
我只是想知道如何制作 NSMutableAttributedString 的副本。我有一个名为 text 的属性,我想在某个时间点保存它的内容,并在发生某些事情时恢复它。我尝试创建一个名为 textC
以下代码在 iOS 上不断崩溃。请帮我找出这个错误的确切原因。 @try { dispatch_async(dispatch_get_global_que
我正在使用 SplitViewController; master 是带有一系列 RightDetail 单元格的 tableView,其属性字符串是通过值设置弹出窗口的回调设置的。 通过设置单元格
我正在使用两个 NSMutableAttributedString 并从这两个中创建一个 NSMutableAttributedString。我想限制两个 attributedString 的不同最大
我正在尝试设置数组中所有范围的颜色,但出现此错误。我不明白为什么。范围都有效。我什至尝试手动插入一个范围来测试它。谢谢。 CGContextSetFillColorWithColor:无效上下文 0x
我需要用字符串 productDesc 初始化 NsmutableAttributedString,但是代码崩溃 attrStrInfoLabel= [[NSMutableAttributedStr
我们的 iOS 应用程序中有一个简单的标记解析器,它获取带有类似 HTML 标签的文本,并通过遍历简单 NSString 中的每个字符将其转换为 NSAttributedString并生成一个 NSM
这个问题在这里已经有了答案: iOS ttf fonts only partially work (3 个答案) 关闭 5 年前。 这很奇怪。 这个有效: titleLabel.font = UIF
我有一个椭圆形的文本路径。对于填充路径,我使用了 NSMutableAttributedString: UIFont *font = [UIFont fontWithName:@"font name"
更新: 我创建了一个非常简单的独立项目来演示该错误。如果有人想拉同样的东西,看看他们是否能发现我哪里出错了,我一定会很感激。没有太多代码可以浏览。公共(public) repo 在这里: https:
我是一名优秀的程序员,十分优秀!