Xamarin.ios monotouch NSAttribute字符串样式/属性测试
我在UITextView中有一个NSAttributed字符串,文本具有多个属性(粗体/斜体等) 我试图检测哪个样式应用于文本的选定部分,以启用或禁用各种按钮:Xamarin.ios monotouch NSAttribute字符串样式/属性测试,xamarin.ios,xamarin,nsattributedstring,Xamarin.ios,Xamarin,Nsattributedstring,我在UITextView中有一个NSAttributed字符串,文本具有多个属性(粗体/斜体等) 我试图检测哪个样式应用于文本的选定部分,以启用或禁用各种按钮: NSRange selectionRange=getselectedtemtrange(); NSMutableAttributedString text=新的NSMutableAttributedString(ActiveTextInput.AttributedText); NSDictionary attributesDiction
NSRange selectionRange=getselectedtemtrange();
NSMutableAttributedString text=新的NSMutableAttributedString(ActiveTextInput.AttributedText);
NSDictionary attributesDictionary=text.GetAttributes(selectionRange.Location,out selectionRange);
text.EnumerateAttributes(selectionRange,NSAttributedStringEnumeration.LongestEffectiveRangeNotRequired,(NSDictionary属性,NSRange范围,ref bool stop)=>
{
if(attributes.ObjectForKey(CTStringAttributeKey.UnderlineStyle).Equals(NSUnderlineStyle.Single))
{
控制台。书写线(“下划线”);
停止=真;
}
其他的
{
控制台。书写线(“非下划线”);
}
});
受保护的NSRange getselectedtextange()
{
NSRange selectionRange=新NSRange(
ActiveTextInput.GetOffsetFromPosition(ActiveTextInput.BeginingOfDocument,ActiveTextInput.SelectedTextRange.start),
ActiveTextInput.GetOffsetFromPosition(ActiveTextInput.SelectedTextRange.start,ActiveTextInput.SelectedTextRange.end)
);
//如果选定范围为空,请将选择器旁边的字符包括在当前范围中
如果(selectionRange.Length.Equals(0))
selectionRange=new NSRange(selectionRange.Location-1,1);
返回选择范围;
}
代码在IF行引发异常:System.NullReferenceException:Object reference未设置为对象的实例
这是测试属性存在性的正确方法吗?此外,如何测试文本是粗体还是带下划线,因为它们是单个属性(CTStringAttributeKey.Font
),我已经检查了一些示例(),但颜色不同,因为它有自己的属性键
我试图实现的是一个具有简单标记的富文本编辑器,就像Word对B/U/I/Color所做的那样。我想这是一个基本的c#问题,当尝试访问.ObjectForKey()
方法时,如果没有在字典中设置键,则会引发NullReference异常,因此只需使用.TryGetValue()
在尝试通过键名访问数据之前我想这是一个基本的c问题,当尝试访问.ObjectForKey()
方法时,如果字典中没有设置键值,则会引发NullReference异常,所以在尝试通过键名访问数据之前,只需使用.TryGetValue()