标签: nsrange

将selectedTextRange UITextRange转换为NSRange

如何将UITextRange对象转换为NSRange?我已经看到很多关于走向另一个方向的SO帖子,但这与我需要的相反.我正在使用的UITextRange selectedTextRange是一个属性UITextView.它返回一个,UITextRange但我需要一个范围.

objective-c ios nsrange uitextrange

24
推荐指数
3
解决办法
1万
查看次数

获取NSString中字符的索引,使用偏移量并使用Objective-C中的子字符串

我有一根绳子!

   NSString *myString=[NSString stringWithFormat:@"This is my lovely string"];
Run Code Online (Sandbox Code Playgroud)

我想做的是:

  1. 假设字符串中的第一个字符是索引0.转到第11个字符(在上面的情况下是'l'),并向后找到第一个出现空格的位置(在上面的字符串中,第一个出现的空格的位置)如果我们从'l'后退到位置10).让我们称这个空间'leSpace'的索引值为10.
  2. 使用...将剩余字符串子串到新字符串

    [myString substringFromIndex:leSpace]
    
    Run Code Online (Sandbox Code Playgroud)

......我希望我解释得很好.请帮助,你能写一个片段或什么来帮助我完成这项任务吗?

substring objective-c nsstring ios nsrange

21
推荐指数
1
解决办法
4万
查看次数

如何使用NSRange创建NSArray的子​​阵列?

我有一个包含内容的数组.像往常一样,它包含20个对象.我希望在Tableview中将相同的数组拆分为2个部分.我试图用当前数组中的NSMake实现它.例如,我需要在第一个tableview部分获取3行,第二个将包含所有其余的(17行).

switch (section) {
        case 0:
            return
            [[array subarrayWithRange:NSMakeRange(3, 8)] count];
            // in this line, it always takes from the first object in array, despite I told hime start from 3 (If I understand right, how to works NSMakeRange)
            break;
        case 1:
            return
            [[array subarrayWithRange:NSMakeRange(9, 19)] count];
            // here my app is crashing with an error 
            //*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray subarrayWithRange:]: range {9, 19} extends beyond bounds [0 .. 19]'
        default:
            break;
    } …
Run Code Online (Sandbox Code Playgroud)

arrays objective-c nsarray ios nsrange

21
推荐指数
2
解决办法
3万
查看次数

如何在NSLog中打印NSRange

假设我的范围被创建为

NSRange myRange = {0,100};
Run Code Online (Sandbox Code Playgroud)

如何myRange在NSLog中打印?以下不起作用

NSLog(@"my range is %@",myRange);
Run Code Online (Sandbox Code Playgroud)

objective-c nslog ios nsrange

21
推荐指数
2
解决办法
5839
查看次数

如何在NSString中获取特定字符的所有NSRange?

我有两个NSStrings:orgTextsearchLetter.
我想orgText用红色突出显示searchLetter的每一个匹配项.
我怎样才能得到NSRange所有出现的searchLetter
例如:

suppose: orgText = "abcahaiapaoiuiapplma"
         searchLetter = "a".
Run Code Online (Sandbox Code Playgroud)

我想高亮显示红色"abcahaiapaoiuiapplma"中的所有"a"事件.
谢谢.

objective-c nsstring ios nsrange

19
推荐指数
2
解决办法
2万
查看次数

NSRangeException Out of Bounds错误

我正在设置标签的属性文本,我收到这个奇怪的错误:由于未捕获的异常'NSRangeException'而终止应用程序,原因:'NSMutableRLEArray replaceObjectsInRange:withObject:length :: Out of bounds'.我之前从未见过这个错误,所以我不确定如何修复它.以下是导致崩溃的代码:

if ([cell.waveTextLabel respondsToSelector:@selector(setAttributedText:)])
{
    const CGFloat fontSize = 16.0f;

    //define attributes
    UIFont *boldFont = [UIFont fontWithName:@"HelveticaNeue-Medium" size:fontSize];

    // Create the attributes
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:boldFont, NSFontAttributeName, nil];

    NSRange rangeOfIs = [cell.cellWaveObject.waveString rangeOfString:@" is"];
    NSLog(@"The range of is: {%lu, %lu}", (unsigned long)rangeOfIs.location, (unsigned long)rangeOfIs.length);
    NSRange nameRange = NSMakeRange(0, rangeOfIs.location);
    NSLog(@"The range of the name: {%lu, %lu}", (unsigned long)nameRange.location, (unsigned long)nameRange.length);

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:cell.cellWaveObject.waveString];
    [attributedString setAttributes:attrs range:nameRange];

    //set the label text
    [cell.waveTextLabel …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring uilabel nsrange nsrangeexception

19
推荐指数
1
解决办法
2182
查看次数

无法将'NSRange'(又名'_NSRange')类型的值转换为预期类型'Range <Index>'(又名'Range <String.CharacterView.Index>')

检查字符串字符的范围时出现此错误...

@objc func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let shouldChange = false
    let text = textField.text
    var newString = text!.stringByReplacingCharactersInRange(range, withString: string) as? NSString
    if newString.length > 14{
        newString = newString.substringToIndex(14)
    }
    textField.text = newString.uppercaseString

    return shouldChange
}
Run Code Online (Sandbox Code Playgroud)

ios nsrange swift

18
推荐指数
1
解决办法
9706
查看次数

NSRange:range.location!= NSNotFound vs. range.length> 0

我正在浏览我的一个应用程序中的一些旧代码,并在可能存在问题的领域修复代码.

我看到很多旧代码使用...

NSRange range = //determine range here....
if(range.length > 0)
{
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

该代码是"罚款",还是应该将其改为此?

NSRange range = //determine range here....
if(range.location != NSNotFound)
{
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

这两种方法基本上是否相同?

objective-c nsstring ios nsrange

14
推荐指数
2
解决办法
2万
查看次数

NSAttributedString和emojis:位置和长度问题

我正在使用NSAttributedString为来自API的文本的某些部分(在Twitter上认为"@mention")着色.

API为我提供了文本和一组实体,这些实体表示应该着色的文本部分(或链接,标签等).

但有时,由于表情符号,颜色会被抵消.


例如,使用此文本:

"@ericd一些文字.@apero"

API给出:

[{"text":"ericd","len":6,"pos":0},{"text":"apero","len":6,"pos":18}]

我使用NSRange成功转换为NSAttributedString:

for m in entities.mentions {
    let r = NSMakeRange(m.pos, m.len)
    myAttributedString.addAttribute(NSForegroundColorAttributeName, value: someValue, range: r)
}
Run Code Online (Sandbox Code Playgroud)

我们认为这"pos": 18是正确的,这是"@apero"开始的地方.正如预期的那样,彩色部分是"@ericd"和"@apero".

但是当在文本中使用表情符号的某些特定组合时,API不能很好地转换为NSATtributedString,颜色是偏移的:

"@ericd一些文字.✌@apero"

得到:

[{"text":"ericd","len":6,"pos":0},{"text":"apero","len":6,"pos":22}]

"pos": 22:API作者声明这是正确的,我理解他们的观点.

不幸的是,NSAttributedString不同意,我的着色是关闭的:

在此输入图像描述

第二次提到的最后一个字符没有着色(因为因为表情符号,"pos"太短了?).

正如您可能已经猜到的那样,我无论如何都无法改变API的行为方式,我必须在客户端进行调整.

除此之外......我不知道该怎么做.我是否应该尝试检测文本中的表情符号,并在有问题的表情符号时手动修改提及的位置?但是,检测哪个表情符号改变位置以及哪个表情符号没有改变的标准是什么?以及如何确定我需要多少偏移?也许问题是由NSAttributedString引起的?

我知道这与表情符号的长度相比,与他们作为离散字符的长度相比,但是......嗯......我迷失了(叹气).


请注意,我已经尝试实现类似于这个东西的解决方案,因为我的API与此兼容,但它只是部分工作,一些表情符号仍在破坏索引:

在此输入图像描述

unicode nsattributedstring emoji nsrange swift

13
推荐指数
1
解决办法
2054
查看次数

Swift 4中的String,substring,Range,NSRange

我使用以下代码String从以下代码获取子字符串NSRange:

func substring(with nsrange: NSRange) -> String? {
    guard let range = Range.init(nsrange)
        else { return nil }
    let start = UTF16Index(range.lowerBound)
    let end = UTF16Index(range.upperBound)
    return String(utf16[start..<end])
}
Run Code Online (Sandbox Code Playgroud)

(来自:https://mjtsai.com/blog/2016/12/19/nsregularexpression-and-swift/)

当我编译与斯威夫特4(9B4的Xcode),我获取该声明两条线以下的错误startend:

'init' is unavailable
'init' was obsoleted in Swift 4.0
Run Code Online (Sandbox Code Playgroud)

我很困惑,因为我没有使用init.

我怎样才能解决这个问题?

string range nsrange swift4

13
推荐指数
2
解决办法
1万
查看次数