Custom Bullets from Images on UITextView or UILabel Swift - swift

I'm creating an app that basically is a mobile version of a large website. This website has a certain area that lists product features. Each feature is bulleted with a unique custom image.
Is there a way to do this with swift? Essentially, create a bullet list of strings, but instead use small images for the bullet dots?

Make your UITextView's text type as attributed
Increase the Head indentation value
Construct your string in the following format:
"• This is a string\n\n• This is another string, but longer and better. The number of words in this string are probably higher than the last one."
Use Alt + 8 to type in a bullet.
Result:

Related

TextKit 2 - How to create word level NSTextElements in order to tag words in text

I'm trying to create tags/labels/badges to tag certain words(actually sometimes multiple words can constitute a tag). Currently, I've added a custom NSAttributedString key that I have used to tag words with their part of speech. I want to tag these words with a rounded background coloring. I've gone through the code in this sample code, that creates tags like this:
But the example code just takes existing NSTextElements(paragraph level) and applies styling to them. When I plug into the same delegate functions from the text code all the text elements are paragraph level. For example
This is one.
This is two.
Would create two text elements: This is one. and This is two.. I'm curious how I can split these into multiple text elements. For example if I wanted to just tag EGG in EGG SANDWICH NO. 2, I'm guessing I need to split that text element into two(EGG and SANDWICH NO. 2), but I can't figure out how to do that currently, or find any examples of adding word tags to text on MacOS(The app does not need to support iOS, only MacOS).
Am I along the right track for accomplishing this? If so, what's the best way to split up the NSTextElements?
I had a similar problem and failed in a way that I really discourage to do that.
TextKit 2 is evolving. I report on trying to create a NSTextElement subclass below the NSTextParagraph level in macOS Ventana (autumn 2022).
My problem was to communicate the layout position of a text attachment, which is part of the attributed string, to the text view to position the associated subview and its exclusion path accordingly - a standard problem for text with figures.
The path to introduce a subclass of an NSTextElement into the text layout process is via delegates.
Starting at the text store:
NSTextContentStoreDelegate offers methods to introduce custom NSTextElements depending on the content of the NSAttributedString:
1. Paragraph Level:
func textContentStorage(NSTextContentStorage, textParagraphWith: NSRange) -> NSTextParagraph?
2. Arbitrary NSTextElement Level:
func textContentManager(NSTextContentManager, textElementAt: NSTextLocation) -> NSTextElement?
NSTextLayoutManagerDelegate can deliver custom NSTextLayoutFragment objects for these custom NSTextElements:
func textLayoutManager( _ textLayoutManager: NSTextLayoutManager, textLayoutFragmentFor location: NSTextLocation, in textElement: NSTextElement) -> NSTextLayoutFragment
The custom NSTextLayoutFragment objects can do what you desire.
I tried to subclass on the NSTextElement level (using method 2 of NSTextContentStoreDelegate) and used a custom NSTextLayoutFragment to execute my code. The result was that the entire layout process stopped when this custom NSTextLayoutFragment object was encountered. I overrode all methods that are in the documentation of the NSTextLayoutFragment to catch the problem and failed. Even worse, I introduced memory problems into my code that Swift as a language should guard against. At places where I and the compiler expected a model object the executing code saw the view which displays this model object.
I finally accepted that I can not go below the paragraph level and subclassed NSTextParagraph and introduced this class using the first and not the second NSTextContentStorageDelegate method. I solved the desired atomisation down to the attachment within the subclasses. That worked immediately and brought the desired functionality in a very precise and efficient way.

Unity ScrollView with multiline string Content height adaption

So, I have a growing string with many "\n". Every app start I will load the total string and every app use some new lines will be added.
The string is inside the Content of a ScrollView. But the Content size is not scaling with the string length. I want the Content to be exactly as high as the text lines go and be scrollable.
I can think of calculating the line count and set the content height manually. But maybe there is a more simple way? Please tell me your solutions.
I found a convenient non-code answer myself. You put the Text-Component directly on your Content GameObject. Then you use a ContentSizeFitter and set Vertical: Preferred. Also the Content anchors have to be set for variable height.
This enables me to dynamically set and scroll through a different text length with a ScrollView. (The font size is kept, to still fit the rest of the GUI)
I am still open for other answers.

Display what AVSpeechSynthesizer is showing as subtitles with Swift

I have an array with quotations, and am using a button to start a random quotation playing from the array.
I'm wondering, how would I have the selected random quotation also displayed in the ViewController so that people can read what the speechsynthesizer is reading?
How would I have the selected random quotation also displayed in the ViewController so that people can read what the speechsynthesizer is reading?
The best way to highlight the vocalized word is using the speechSynthesizer:willSpeakRangeOfSpeechString:utterance: method of the AVSpeechSynthesizerDelegate protocol.
Instead of copying and pasting the code snippet here, take a look at this complete and useful example (ObjC and Swift) that displays the vocalized word in a bold font with the speech synthesis.
...is there some way of using Subtitles.sizeToFit() so that the Label is split into 3 or more rows, instead of just "one row till infinity"?
In the Xcode Interface Builder - Attributes Inspector, set the value of the label property Lines to 0 and adapt your constraints to get a multiline presentation.
Add a label to your view and and set the quote text to it
quoteLabel.text = randomQuote
To show your label on multiple rows add:
label.numberOfLines = 0

How do you createa a Material Text Area in Angular Dart?

I would like to create a large, multiline text area using material components in Angular Dart. I know I can do:
<material-input multiline></material-input>
But that starts life as a single line input and grows as the user types. I'm looking for a large text area of fixed size that scrolls as the user types (as explained https://material.io/design/components/text-fields.html#input-types)
Is this possible with Angular Dart's material components?
Sorry if I am not completely understanding your question, but I think you can get what you want by providing a row, and maxRow value.
Example:
<material-input multiline rows="2" maxRows="2"></material-input>
Would create a input that started as two rows high and then didn't grow at all, but scrolled instead.

How to slice text or html string into pages with iPhone SDK?

How to slice some text (html) string into number of pages to be possible read text as a book?
Thanks for suggestions.
Assuming you are happy recognising only a subset of HTML markup without CSS (here I assume <p/><b/><i/><br/> tags only plus <font size=/> for font size changes (with other attributes ignored), <img> tags for images with all but src,width,height ignored and accurate width and height mandatory with all other tags/attributes ignored):-
TidyLib seems to have an MIT license - http://tidy.sourceforge.net/#source
SAX parse the XHTML output of TidyLib using NSXmlParser into a custom object model (unless you are exclusively using later versions of iPhone OS with public builtin DOM parser API in which case just use a DOM object model).
Set up a state machine with a caret position at top left of page and initial font size and formatting, page number of 1, maximum height of glyphs/images in current line of zero, and empty list of page boundaries.
For each run of text or image in object model, apply pre-ceding font size/format modifications, measure text using iPhone text measurement calls, reducing text length (trim to nearest space or hyphen) until it fits on current line, and resetting caret to line beginning and continuing for line wraps, and apply following font size and formatting changes. Over-count the width and height of text by some factor in cases where this is found to be required to prevent page overflow in the actual page rendering engine (UIWebView; you will have to experiment to see what the factors in the rendering engine are). Record page boundary in list.
Convert objects between page boundaries to simplified XHTML for each page. You may wish to add some CSS at this point for example to format link colours. You will need to convert local references to anchors on another page to load the correct other page. Perhaps add page footer/header with page numbers (subtract size of these from page height in earlier steps).
Save XHTML as set of files.
In essence this will work as long as the source HTML is specially prepared to use a subset of HTML for your app. Any old HTML will not do, though it might perhaps not be completely useless to give a rough idea for previews in some instances for some files.
The description above assumes you throw away formatting like ALIGN= and tables. It really is a very basic approach and will not reproduce complex pages as originally designed! It might well not suit you!
Perhaps the files should be pre-processed before reaching the iPhones in the field but if the iPhone OS / WebView line-wrapping/test positioning behaviour changes, the best position for page breaks may change. So you may need to cut your pages smaller than you think they need to be to allow for some unexpected growth when the rendering engine changes. Hmm. Perhaps not an easy task!
I haven't even tried to analyse HTML tables... HTML is of course, in its non-restricted full glory enormously probably unmanageably complex.