private void FeedTags(List <string> tagNames, UIButton btn) { // Construct tags attributed strings: NSMutableAttributedString text = null; foreach (var tag in tagNames) { var chip = NSAttributedString.CreateFrom(new NSTextAttachment { Image = ServiceContainer.Resolve <TagChipCache> ().Get(tag, btn), }); if (text == null) { text = new NSMutableAttributedString(chip); } else { text.Append(new NSAttributedString(" ", Style.EditTimeEntry.WithTags)); text.Append(chip); } } if (text == null) { btn.SetAttributedTitle(new NSAttributedString("EditEntryTagsHint".Tr(), Style.EditTimeEntry.NoTags), UIControlState.Normal); } else { btn.SetAttributedTitle(text, UIControlState.Normal); } }
private void ConfigureTextView() { TextView.Font = UIFont.FromDescriptor(UIFontDescriptor.PreferredBody, 0); TextView.TextColor = UIColor.Black; TextView.BackgroundColor = UIColor.White; TextView.ScrollEnabled = true; // Let's modify some of the attributes of the attributed string. // You can modify these attributes yourself to get a better feel for what they do. // Note that the initial text is visible in the storyboard. var attributedText = new NSMutableAttributedString(TextView.AttributedText); // Use NSString so the result of rangeOfString is an NSRange, not Range<String.Index>. NSString text = (NSString)TextView.Text; // Find the range of each element to modify. var boldRange = CalcRangeFor(text, "bold".Localize()); var highlightedRange = CalcRangeFor(text, "highlighted".Localize()); var underlinedRange = CalcRangeFor(text, "underlined".Localize()); var tintedRange = CalcRangeFor(text, "tinted".Localize()); // Add bold. Take the current font descriptor and create a new font descriptor with an additional bold trait. var boldFontDescriptor = TextView.Font.FontDescriptor.CreateWithTraits(UIFontDescriptorSymbolicTraits.Bold); var boldFont = UIFont.FromDescriptor(boldFontDescriptor, 0); attributedText.AddAttribute(UIStringAttributeKey.Font, value: boldFont, range: boldRange); // Add highlight. attributedText.AddAttribute(UIStringAttributeKey.BackgroundColor, value: ApplicationColors.Green, range: highlightedRange); // Add underline. attributedText.AddAttribute(UIStringAttributeKey.UnderlineStyle, value: NSNumber.FromInt32((int)NSUnderlineStyle.Single), range: underlinedRange); // Add tint. attributedText.AddAttribute(UIStringAttributeKey.ForegroundColor, value: ApplicationColors.Blue, range: tintedRange); // Add image attachment. var img = UIImage.FromBundle("text_view_attachment"); var textAttachment = new NSTextAttachment { Image = img, Bounds = new CGRect(PointF.Empty, img.Size), }; var textAttachmentString = NSAttributedString.CreateFrom(textAttachment); attributedText.Append(textAttachmentString); TextView.AttributedText = attributedText; }
private void Initialize() { var attachment = new NSTextAttachment { Image = Image, Bounds = new CGRect(-Insets.Left, ImageYPosition, ImageSize.Height, ImageSize.Width) }; var newText = new NSMutableAttributedString(); newText.Append(NSAttributedString.CreateFrom(attachment)); NSAttributedString labelText = new NSAttributedString(Text ?? " "); newText.Append(labelText); AttributedText = newText; }
private void BindTagsButton(UIButton v) { if (tagsView == null) { return; } // Construct tags attributed strings: NSMutableAttributedString text = null; foreach (var tag in tagsView.Data) { if (String.IsNullOrWhiteSpace(tag)) { continue; } var chip = NSAttributedString.CreateFrom(new NSTextAttachment() { Image = ServiceContainer.Resolve <TagChipCache> ().Get(tag, v), }); if (text == null) { text = new NSMutableAttributedString(chip); } else { text.Append(new NSAttributedString(" ", Style.EditTimeEntry.WithTags)); text.Append(chip); } } if (text == null) { v.SetAttributedTitle(new NSAttributedString("EditEntryTagsHint".Tr(), Style.EditTimeEntry.NoTags), UIControlState.Normal); } else { v.SetAttributedTitle(text, UIControlState.Normal); } }
private void ConfigureTextView() { textView.Font = UIFont.FromDescriptor(UIFontDescriptor.PreferredBody, 0); textView.TextColor = UIColor.Black; textView.BackgroundColor = UIColor.White; textView.ScrollEnabled = true; // Let's modify some of the attributes of the attributed string. // You can modify these attributes yourself to get a better feel for what they do. // Note that the initial text is visible in the storyboard. var attributedText = new NSMutableAttributedString(textView.AttributedText); // Find the range of each element to modify. var text = textView.Text; var boldRange = GetRangeFor(text, "bold"); var highlightedRange = GetRangeFor(text, "highlighted"); var underlinedRange = GetRangeFor(text, "underlined"); var tintedRange = GetRangeFor(text, "tinted"); // Add bold. Take the current font descriptor and create a new font descriptor with an additional bold trait. var boldFontDescriptor = textView.Font.FontDescriptor.CreateWithTraits(UIFontDescriptorSymbolicTraits.Bold); var boldFont = UIFont.FromDescriptor(boldFontDescriptor, 0); attributedText.AddAttribute(UIStringAttributeKey.Font, boldFont, boldRange); // Add highlight. attributedText.AddAttribute(UIStringAttributeKey.BackgroundColor, UIColor.Green, highlightedRange); // Add underline. attributedText.AddAttribute(UIStringAttributeKey.UnderlineStyle, NSNumber.FromInt32((int)NSUnderlineStyle.Single), underlinedRange); // Add tint. attributedText.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Blue, tintedRange); // Add image attachment. var image = UIImage.FromBundle("text_view_attachment"); var textAttachment = new NSTextAttachment { Image = image, Bounds = new CGRect(PointF.Empty, image.Size), }; var textAttachmentString = NSAttributedString.CreateFrom(textAttachment); attributedText.Append(new NSAttributedString(Environment.NewLine)); attributedText.Append(textAttachmentString); textView.AttributedText = attributedText; image.Dispose(); attributedText.Dispose(); image = null; attributedText = null; NSRange GetRangeFor(string source, string substring) { return(new NSRange { Location = source.IndexOf(substring, StringComparison.OrdinalIgnoreCase), Length = substring.Length }); } }