Esempio n. 1
0
        private static void addProjectAttachmentsIfNeeded(TextFieldInfo info, NSMutableAttributedString finalString)
        {
            if (string.IsNullOrEmpty(info.ProjectColor))
            {
                return;
            }

            var color = MvxColor.ParseHexString(info.ProjectColor).ToNativeColor();

            var projectName = new NSAttributedString(info.ProjectName.TruncatedAt(maxTextLength), new UIStringAttributes
            {
                Font            = tokenFont,
                ForegroundColor = color
            });

            var textAttachment = new TokenTextAttachment(
                projectName,
                textVerticalOffset,
                color,
                regularFont.Descender,
                textFieldInfoTokenLeftMargin,
                textFieldInfoTokenRightMargin
                );
            var tokenString = new NSMutableAttributedString(NSAttributedString.FromAttachment(textAttachment));
            var attributes  = new UIStringAttributes {
                ParagraphStyle = paragraphStyle
            };

            attributes.Dictionary[Project] = new NSObject();
            tokenString.AddAttributes(attributes, new NSRange(0, tokenString.Length));

            finalString.Append(tokenString);
        }
Esempio n. 2
0
        public static NSAttributedString GetAttributedStringWithImage(string imageName, int imageSize, string text)
        {
            var attrString = new NSMutableAttributedString("");

            if (!string.IsNullOrEmpty(imageName))
            {
                var image = GetImageForSize(imageName, imageSize);
                if (image != null)
                {
                    image.AlignmentRect = new CGRect {
                        X = 0, Y = 5, Width = imageSize - 2, Height = imageSize - 2
                    };
                    var cell = new NSTextAttachmentCell(image);
                    cell.Alignment = NSTextAlignment.Left;
                    attrString.Append(NSAttributedString.FromAttachment(new NSTextAttachment {
                        AttachmentCell = cell
                    }));
                    attrString.Append(new NSAttributedString("  "));
                }
            }

            attrString.Append(new NSAttributedString(text));

            var style = new NSMutableParagraphStyle {
                LineBreakMode = NSLineBreakMode.TruncatingMiddle
            };

            attrString.AddAttribute(NSStringAttributeKey.ParagraphStyle, style, new NSRange(0, attrString.Length));

            return(attrString);
        }
Esempio n. 3
0
        public static NSMutableAttributedString GetAttachmentString(
            this string imageName,
            double fontCapHeight,
            UIImageRenderingMode renderingMode = UIImageRenderingMode.AlwaysTemplate)
        {
            var attachment = new NSTextAttachment
            {
                Image = UIImage
                        .FromBundle(imageName)
                        .ImageWithRenderingMode(renderingMode)
            };

            var imageSize = attachment.Image.Size;
            var y         = (fontCapHeight - imageSize.Height) / 2;

            attachment.Bounds = new CGRect(0, y, imageSize.Width, imageSize.Height);

            //There neeeds to be a space before the dot, otherwise the colors don't work
            var result = new NSMutableAttributedString(" ");

            result.AddAttribute(UIStringAttributeKey.Font, UIFont.SystemFontOfSize(0), new NSRange(0, 1));

            var attachmentString = NSAttributedString.FromAttachment(attachment);

            result.Append(attachmentString);

            return(result);
        }
        private static NSMutableAttributedString AsAttributedString(this ProjectSpan projectSpan)
        {
            var projectColor = MvxColor.ParseHexString(projectSpan.ProjectColor).ToNativeColor();
            var projectName  = new NSAttributedString(projectSpan.ProjectName.TruncatedAt(maxTextLength), new UIStringAttributes
            {
                Font            = tokenFont,
                ForegroundColor = projectColor
            });

            var textAttachment = new TokenTextAttachment(
                projectName,
                textVerticalOffset,
                projectColor,
                regularFont.Descender,
                textFieldInfoTokenLeftMargin,
                textFieldInfoTokenRightMargin
                );

            var tokenString = new NSMutableAttributedString(NSAttributedString.FromAttachment(textAttachment));
            var attributes  = createBasicAttributes();

            attributes.Dictionary[ProjectId]    = new NSNumber(projectSpan.ProjectId);
            attributes.Dictionary[ProjectName]  = new NSString(projectSpan.ProjectName);
            attributes.Dictionary[ProjectColor] = new NSString(projectSpan.ProjectColor);
            if (projectSpan.TaskId.HasValue)
            {
                attributes.Dictionary[TaskId]   = new NSNumber(projectSpan.TaskId.Value);
                attributes.Dictionary[TaskName] = new NSString(projectSpan.TaskName);
            }

            tokenString.AddAttributes(attributes, new NSRange(0, tokenString.Length));

            return(tokenString);
        }
        private static NSMutableAttributedString AsAttributedString(this ProjectSpan projectSpan)
        {
            var projectColor = MvxColor.ParseHexString(projectSpan.ProjectColor).ToNativeColor();

            var projectNameString = projectSpan.ProjectName;

            if (!string.IsNullOrEmpty(projectSpan.TaskName))
            {
                projectNameString = $"{projectNameString}: {projectSpan.TaskName}";
            }

            var textAttachment = new ProjectTextAttachment(
                projectNameString, projectColor, tokenFont);

            var tokenString = new NSMutableAttributedString(NSAttributedString.FromAttachment(textAttachment));
            var attributes  = createBasicAttributes();

            attributes.Dictionary[ProjectId]    = new NSNumber(projectSpan.ProjectId);
            attributes.Dictionary[ProjectName]  = new NSString(projectSpan.ProjectName);
            attributes.Dictionary[ProjectColor] = new NSString(projectSpan.ProjectColor);
            if (projectSpan.TaskId.HasValue)
            {
                attributes.Dictionary[TaskId]   = new NSNumber(projectSpan.TaskId.Value);
                attributes.Dictionary[TaskName] = new NSString(projectSpan.TaskName);
            }

            tokenString.AddAttributes(attributes, new NSRange(0, tokenString.Length));

            return(tokenString);
        }
Esempio n. 6
0
        public static NSMutableAttributedString GetAttachmentString(
            this string imageName,
            double fontCapHeight,
            UIImageRenderingMode renderingMode = UIImageRenderingMode.AlwaysTemplate)
        {
            var attachment = new NSTextAttachment
            {
                Image = UIImage
                        .FromBundle(imageName)
                        .ImageWithRenderingMode(renderingMode)
            };

            var imageSize = attachment.Image.Size;
            var y         = (fontCapHeight - imageSize.Height) / 2;

            attachment.Bounds = new CGRect(0, y, imageSize.Width, imageSize.Height);

            var result = new NSMutableAttributedString("");

            if (!UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                // For older iOS versions, there neeeds to be a space before the dot, otherwise the colors don't work
                // in iOS 13, they fixed it and this hack breaks the colors
                result.Append(new NSAttributedString(" "));
                result.AddAttribute(UIStringAttributeKey.Font, UIFont.SystemFontOfSize(0), new NSRange(0, 1));
            }

            var attachmentString = NSAttributedString.FromAttachment(attachment);

            result.Append(attachmentString);

            return(result);
        }
Esempio n. 7
0
 void InsertCat(NSTextAttachmentCell cell)
 {
     textView.TextStorage.Append(
         NSAttributedString.FromAttachment(
             new NSTextAttachment {
         AttachmentCell = cell
     }
             )
         );
 }
        public void UIKitAttachmentConveniences_New()
        {
            TestRuntime.AssertSystemVersion(PlatformName.iOS, 7, 0, throwIfOtherPlatform: false);
            TestRuntime.AssertSystemVersion(PlatformName.MacOSX, 10, 11, throwIfOtherPlatform: false);

            // so we added custom code calling the (old) category helper - but we had to pick a different name
            using (var ta = new NSTextAttachment(null, null))
                using (var as2 = NSAttributedString.FromAttachment(ta)) {
                    Assert.That(as2.Length, Is.EqualTo((nint)1), "Length");
                    Assert.That(as2.Value [0], Is.EqualTo((char)0xFFFC), "NSAttachmentCharacter");
                }
        }
        private NSAttributedString buildString(string project, string task, string client, UIColor color, bool isTaskPlaceholder)
        {
            var builder   = new StringBuilder();
            var hasClient = !string.IsNullOrEmpty(client);

            if (!string.IsNullOrEmpty(project))
            {
                builder.Append($" {project}");
            }

            if (!string.IsNullOrEmpty(task) && !isTaskPlaceholder)
            {
                builder.Append($": {task}");
            }

            if (hasClient)
            {
                builder.Append($" {client}");
            }

            var text = builder.ToString();

            var result      = new NSMutableAttributedString(text);
            var clientIndex = text.Length - (client?.Length ?? 0);

            var projectNameRange      = new NSRange(0, clientIndex);
            var projectNameAttributes = new UIStringAttributes {
                ForegroundColor = color
            };

            result.AddAttributes(projectNameAttributes, projectNameRange);

            if (hasClient)
            {
                var clientNameRange      = new NSRange(clientIndex, client.Length);
                var clientNameAttributes = new UIStringAttributes {
                    ForegroundColor = clientColor
                };
                result.AddAttributes(clientNameAttributes, clientNameRange);
            }

            if (isTaskPlaceholder)
            {
                var attachment = new NSTextAttachment();
                attachment.Image = placeHolderImage();
                var placeholderImage = new NSMutableAttributedString(" ");
                placeholderImage.Append(NSAttributedString.FromAttachment(attachment));
                result.Append(placeholderImage);
                return(result);
            }

            return(result);
        }
        // Insert desired image in text, generating length or by replacing a character.
        void InsertImageInText(UITextView textView, string filename, UIImage image, nint position, bool replaceCharacter)
        {
            var attachment = new NSCustomTextAttachment {
                Image = image,
                Name  = filename
            };

            var attributedImage = NSAttributedString.FromAttachment(attachment);
            var textWithImage   = new NSMutableAttributedString(textView.AttributedText);

            textWithImage.Replace(new NSRange(position, replaceCharacter ? 1 : 0), attributedImage);

            textView.AttributedText = textWithImage;
        }
Esempio n. 11
0
        public override void LoadView()
        {
            View = new UIView(UIScreen.MainScreen.Bounds)
            {
                BackgroundColor = UIColor.GroupTableViewBackgroundColor
            };

            var height = UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad ? 450 : 300;

            Calendar = new FSCalendar()
            {
                Frame                   = new CGRect(0, NavigationController.NavigationBar.Frame.GetMaxY(), View.Frame.Width, height),
                DataSource              = this,
                WeakDelegate            = this,
                AllowsMultipleSelection = true
            };
            Calendar.SwipeToChooseGesture.Enabled = true;
            View.AddSubview(Calendar);

            Calendar.CalendarHeaderView.BackgroundColor  = UIColor.LightGray.ColorWithAlpha(0.1f);
            Calendar.CalendarWeekdayView.BackgroundColor = UIColor.LightGray.ColorWithAlpha(0.1f);
            Calendar.FSAppearance.EventSelectionColor    = UIColor.White;
            Calendar.FSAppearance.EventOffset            = new CGPoint(0, -7);
            Calendar.Today = null; // Hide the today circle
            Calendar.RegisterClass(new ObjCRuntime.Class(typeof(DIYCalendarCell)), "cell");
            var scopeGesture = new UIPanGestureRecognizer((UIPanGestureRecognizer obj) => {
                Calendar.HandleScopeGesture(obj);
            });

            Calendar.AddGestureRecognizer(scopeGesture);

            EventLabel = new UILabel(new CGRect(0, Calendar.Frame.GetMaxY() + 10, View.Frame.Width, 50))
            {
                TextAlignment = UITextAlignment.Center,
                Font          = UIFont.GetPreferredFontForTextStyle(UIFontTextStyle.Subheadline)
            };
            View.AddSubview(EventLabel);

            var attributedText = new NSMutableAttributedString("");
            var attatchment    = new NSTextAttachment()
            {
                Image = UIImage.FromBundle("icon_cat")
            };

            attatchment.Bounds = new CGRect(0, -3, attatchment.Image.Size.Width, attatchment.Image.Size.Height);
            attributedText.Append(NSAttributedString.FromAttachment(attatchment));
            attributedText.Append(new NSAttributedString(" This is a daily event "));
            attributedText.Append(NSAttributedString.FromAttachment(attatchment));
            EventLabel.AttributedText = attributedText;
        }
Esempio n. 12
0
        private static NSMutableAttributedString AsAttributedString(this TagSpan tagSpan)
        {
            var tagName        = new NSMutableAttributedString(tagSpan.TagName.TruncatedAt(maxTextLength), tagAttributes);
            var textAttachment = new TagTextAttachment(tagName, textVerticalOffset, regularFont.Descender);
            var tokenString    = new NSMutableAttributedString(NSAttributedString.FromAttachment(textAttachment));

            var attributes = createBasicAttributes();

            attributes.Dictionary[TagId]   = new NSNumber(tagSpan.TagId);
            attributes.Dictionary[TagName] = new NSString(tagSpan.TagName);
            tokenString.AddAttributes(attributes, new NSRange(0, tokenString.Length));

            return(tokenString);
        }
        private static NSMutableAttributedString AsAttributedString(this TagSpan tagSpan)
        {
            var tagName        = tagSpan.TagName.TruncatedAt(maxTextLength);
            var textAttachment = tagName.GetTagToken();
            var tokenString    = new NSMutableAttributedString(NSAttributedString.FromAttachment(textAttachment));

            var attributes = createBasicAttributes();

            attributes.Dictionary[TagId]   = new NSNumber(tagSpan.TagId);
            attributes.Dictionary[TagName] = new NSString(tagSpan.TagName);
            tokenString.AddAttributes(attributes, new NSRange(0, tokenString.Length));

            return(tokenString);
        }
Esempio n. 14
0
        public void UIKitAttachmentConveniences_New()
        {
            if (!TestRuntime.CheckSystemAndSDKVersion(7, 0))
            {
                Assert.Inconclusive("requires iOS7+");
            }

            // so we added custom code calling the (old) category helper - but we had to pick a different name
            using (var ta = new NSTextAttachment(null, null))
                using (var as2 = NSAttributedString.FromAttachment(ta)) {
                    Assert.That(as2.Length, Is.EqualTo((nint)1), "Length");
                    Assert.That(as2.Value [0], Is.EqualTo((char)0xFFFC), "NSAttachmentCharacter");
                }
        }
        private static NSAttributedString createAttributedString(
            IEnumerable <TokenTextAttachment> tagTokens)
        {
            var tagTokenString = tagTokens.Aggregate(
                new NSMutableAttributedString(),
                (result, token) =>
            {
                result.Append(NSAttributedString.FromAttachment(token));
                return(result);
            }
                );

            return(tagTokenString);
        }
Esempio n. 16
0
        private void SetupStatusDisplayTextField()
        {
            NSTextAttachment attachment = new NSTextAttachment
            {
                Image  = NSImage.ImageNamed(NSImageName.ApplicationIcon),
                Bounds = CoreGraphics.CGRect.FromLTRB(0, -4, 14, 10),
            };
            NSAttributedString imageAttributedString = NSAttributedString.FromAttachment(attachment);

            NSMutableAttributedString statusDisplayAttributedString = new NSMutableAttributedString();

            statusDisplayAttributedString.Append(imageAttributedString);
            statusDisplayAttributedString.Append(new NSAttributedString("  Apple Cider 2020 for Mac"));

            StatusDisplayTextField.AttributedStringValue = statusDisplayAttributedString;
        }
Esempio n. 17
0
        /// <summary>
        /// 插入图片到textView
        /// </summary>
        /// <param name="bytes">Stream.</param>
        private void AddImageToTextView(byte[] bytes, string originSizeUrl)
        {
            this.InvokeOnMainThread(() =>
            {
                using (var stream = new MemoryStream(bytes))
                {
                    ImageCell cell = new ImageCell(NSImage.FromStream(stream));
                    cell.SetData("picUrl", originSizeUrl);

                    NSTextAttachment am = new NSTextAttachment
                    {
                        AttachmentCell = cell
                    };
                    NSAttributedString nbs = NSAttributedString.FromAttachment(am);
                    this.TextPrint.TextStorage.Append(nbs);
                }
            });
        }
        public NSAttributedString Convert(string project, string task, string client, UIColor color, bool isProjectPlaceholder = false, bool isTaskPlaceholder = false)
        {
            if (isProjectPlaceholder)
            {
                var attachment = new NSTextAttachment();
                attachment.Image = placeHolderImage();
                var placeholderImage = NSAttributedString.FromAttachment(attachment);
                return(placeholderImage);
            }

            var dotString    = projectDotImageResource.GetAttachmentString(fontHeight);
            var clone        = new NSMutableAttributedString(dotString);
            var dottedString = tryAddColorToDot(clone, color);
            var projectInfo  = buildString(project, task, client, color, isTaskPlaceholder);

            dottedString.Append(projectInfo);

            return(dottedString);
        }
Esempio n. 19
0
        public static NSAttributedString GetMultiLineAttributedStringWithImage(NSImage image, string title, string description, nfloat fontSize, NSColor titleColor, NSColor descriptionColor)
        {
            var attrString = new NSMutableAttributedString("");

            if (image != null)
            {
                var cell = new NSTextAttachmentCell(image);
                image.AlignmentRect = new CoreGraphics.CGRect(0, 5, image.Size.Width, image.Size.Height);
                cell.Alignment      = NSTextAlignment.Natural;
                attrString.Append(NSAttributedString.FromAttachment(new NSTextAttachment {
                    AttachmentCell = cell
                }));
                attrString.Append(new NSAttributedString("  "));
            }

            var pstyle = new NSMutableParagraphStyle();

            pstyle.HeadIndent = pstyle.FirstLineHeadIndent = 24;
            attrString.Append(GetMultiLineAttributedString(title, description, fontSize, titleColor, descriptionColor, pstyle));

            return(attrString);
        }
Esempio n. 20
0
        public void SetTexts(string mainText, string moreText, string showMoreText, UIImage showMoreImage)
        {
            var text = mainText;

            if (moreText.Length > 0)
            {
                text += "\n\n" + moreText;
            }
            TextLabel.Text = text;

            var attributed = new NSMutableAttributedString(showMoreText + " ");
            var attachment = new NSTextAttachment {
                Image = showMoreImage
            };

            attributed.Append(NSAttributedString.FromAttachment(attachment));

            PerformWithoutAnimation(() =>
            {
                ShowMoreButton.SetAttributedTitle(attributed, UIControlState.Normal);
                ShowMoreButton.LayoutIfNeeded();
            });
        }
Esempio n. 21
0
        public static NSAttributedString AttributedStringForTextComponents(ICollection <TextComponent> textComponents, bool usePrimaryColor)
        {
            var attributedString   = new NSMutableAttributedString();
            var font               = UIFont.SystemFontOfSize(15);
            var primaryTextColor   = usePrimaryColor ? Color.PRIMARY_TEXT_COLOR.ToUIColor() : Color.SECONDARY_TEXT_COLOR.ToUIColor();
            var secondaryTextColor = Color.SECONDARY_TEXT_COLOR.ToUIColor();

            foreach (var component in textComponents)
            {
                if (component.PrimaryText != null)
                {
                    var translatedText = NSBundle.MainBundle.LocalizedString(component.PrimaryText, "");
                    attributedString.Append(new NSAttributedString(translatedText, foregroundColor: primaryTextColor, font: font));
                }
                else if (component.SecondaryText != null)
                {
                    var translatedText = NSBundle.MainBundle.LocalizedString(component.SecondaryText, "");
                    attributedString.Append(new NSAttributedString(translatedText, foregroundColor: secondaryTextColor, font: font));
                }
                else if (component.IconIdentifier != TextComponent.Icon.None)
                {
                    string icon = "info";
                    switch (component.IconIdentifier)
                    {
                    case TextComponent.Icon.Info:
                        icon = "info";
                        break;

                    case TextComponent.Icon.Location:
                        icon = "location";
                        break;

                    case TextComponent.Icon.Person:
                        icon = "person";
                        break;

                    case TextComponent.Icon.Subject:
                        icon = "book";
                        break;
                    }

                    var iconImage      = UIImage.FromBundle(icon);
                    var iconAttachment = new NSTextAttachment
                    {
                        Image  = iconImage,
                        Bounds = new CoreGraphics.CGRect(new CoreGraphics.CGPoint(0, font.Descender), iconImage.Size),
                    };

                    var attributedIconString = NSAttributedString.FromAttachment(iconAttachment);

                    // Fix for text attachment tinting bug https://stackoverflow.com/a/36319475/1668608
                    attributedString.Append(new NSAttributedString(" ", UIFont.SystemFontOfSize(0)));

                    attributedString.Append(attributedIconString);
                    attributedString.AddAttributes(new UIStringAttributes {
                        ForegroundColor = secondaryTextColor
                    },
                                                   new NSRange(attributedString.Length - attributedIconString.Length - 1, attributedIconString.Length + 1));

                    attributedString.Append(new NSAttributedString(" ", font));
                }
            }

            var paragraphStyle = new NSMutableParagraphStyle
            {
                ParagraphSpacing = 0.25F * font.LineHeight
            };

            attributedString.AddAttributes(new UIStringAttributes {
                ParagraphStyle = paragraphStyle
            },
                                           new NSRange(0, attributedString.Length - 1));

            return(attributedString);
        }