如何在UITextView中制作带圆圈的数字

时间:2013-08-07 23:05:37

标签: ios cocoa-touch uitextview

请帮助......我不确定是否要开始

..如何在我的UITextView

中显示围绕它们的圈子的数字

喜欢1 2 3 4 .............但圈内的每个数字,例如http://openclipart.org/people/gsagri04/GS_Numbers.svg

我正在跳跃从阵列中获取数字并在屏幕上显示它们......但是每个数字都生活在一个像乐透号码的圈子里

@当下我只有一个An数组[1,2,3,4],...一个按钮.....和UItextView来显示最终输出

xcode 4。

2 个答案:

答案 0 :(得分:2)

如果我理解您的需要,您希望在文本视图中显示以下特殊字符:

①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑯⑰⑱⑲⑳⑳

最简单的方法是复制+粘贴这些字符,并替换您需要在textview中显示的内容字符串中的数字字符。为了代码重用,您可以编写一个NSString类别方法来处理此作业。

答案 1 :(得分:0)

@Chris Chen的解决方案非常漂亮,因为你可以随时改变角色。但是,如果您不想使用这些字符,可以使用以下代码将圆圈添加到textView。

UITextPosition *pos = textView.endOfDocument;// textView ~ UITextView

for (int i=0;i<words*2-1;i++){// *2 since UITextGranularityWord considers a whitespace to be a word, words = number of words in the textView.

UITextPosition *pos2 = [textView.tokenizer positionFromPosition:pos toBoundary:UITextGranularityWord inDirection:UITextLayoutDirectionLeft];
UITextRange *range = [textView textRangeFromPosition:pos toPosition:pos2];
CGRect resultFrame = [textView firstRectForRange:(UITextRange *)range ];

if (check whether word at this text position is a number){
     UIView* circleView = [[UIView alloc] initWithFrame:resultFrame];
    circleView.backgroundColor = [UIColor clearColor];
    circleView.layer.borderColor = <color of your choice, probably same as text color>;
     circleView.layer.borderWidth = <the width you want to set the border thickness to>;
    circleView.layer.cornerRadius = <a float value that makes the rectangle look like a circle>;
    circleView .tag = 125;
    [textView circleView ];
}
pos = pos2;

}

代码应放在UITextView委托方法textViewDidChange中。并确保在所有此代码之前删除圆圈视图,因此标记(125)。