Custom Bullets from Images on Uitextview or Uilabel Swift

Bullet in UILabel

You should take a look at AliSoftware's OHAttributedLabel. It is a subclass of UILabel that draws an NSAttributedString

try this:

#import "OHAttributedLabel.h"

#import "NSAttributedString+Attributes.h"

OHAttributedLabel *myLabel=[[OHAttributedLabel alloc]init];
[myLabel setFrame:CGRectMake(0, 0, 70, 20)];
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:@"\u2022 list item!!!"];
[attrStr setTextColor:[UIColor redColor]];
[attrStr setTextColor:[UIColor blackColor] range:NSMakeRange(0,1)];
myLabel.attributedText = attrStr;
[self.view addSubview:myLabel];

if you want to add just bullet than use this:

myLabel.text = @"\u2022  list item!!!!!!!!!";

List of more unicode

Format UILabel with bullet points?

Perhaps use the Unicode code point for the bullet character in your string?

Objective-c

myLabel.text = @"\u2022 This is a list item!";

Swift 4

myLabel.text = "\u{2022} This is a list item!"

Swift 4.2: UITextField secureEntryText using Image instead of Default Bullets

After two days struggling with this, I come to solution that can trick this. I'm creating stackView that contain of UITextField. I create array for temporary text that filled the UITextField. Then, when the UITextField is filled by some character, I programmatically add UIImage. In my code, will look like this.

let chars = Array(shieldString)
for (index, char) in chars.enumerated() {
if let tmp = self.stackView.arrangedSubviews[index] as? UITextField {
let imageView = UIImageView(image: UIImage(named: "dotImage")!)
imageView.frame = CGRect(x: 0, y: 0, width: 28, height: 28)
imageView.center = CGPoint(x: tmp.frame.size.width / 2, y: tmp.frame.size.height / 2)
imageView.tag = 100
tmp.addSubview(imageView)
}
}

And for clear the Image, just using this. I use tag so I can delete specific subview.

for ele in self.stackView.arrangedSubviews {
if let viewWithTag = self.view.viewWithTag(100) {
viewWithTag.removeFromSuperview()
}
}

I know that this not custom class, and quiet messy, but if you in a rush like me, this probably will save you.

How do I add an unordered list or a bullet point every new line in a UITextView

The problem is that you're using

if ([myTextField.text isEqualToString:@"\n"]) {

as your conditional, so the block executes if the entirety of your myTextField.text equals "\n". But the entirety of your myTextField.text only equals "\n" if you haven't entered anything but "\n". That's why right now, this code is only working "the first time the user presses enter"; and when you say "I can't even backspace it," the problem is actually that the bullet point's being re-added with the call to textViewDidChange: since the same conditional is still being met.

Instead of using textViewDidChange: I recommend using shouldChangeTextInRange: in this case so you can know what that replacement text is no matter it's position within the UITextView text string. By using this method, you can automatically insert the bullet point even when the newline is entered in the middle of the block of text... For example, if the user decides to enter a bunch of info, then jump back up a few lines to enter some more info, then tries to press newline in between, the following should still work. Here's what I recommend:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

// If the replacement text is "\n" and the
// text view is the one you want bullet points
// for
if ([text isEqualToString:@"\n"]) {

// If the replacement text is being added to the end of the
// text view, i.e. the new index is the length of the old
// text view's text...
if (range.location == textView.text.length) {
// Simply add the newline and bullet point to the end
NSString *updatedText = [textView.text stringByAppendingString:@"\n\u2022 "];
[textView setText:updatedText];
}

// Else if the replacement text is being added in the middle of
// the text view's text...
else {

// Get the replacement range of the UITextView
UITextPosition *beginning = textView.beginningOfDocument;
UITextPosition *start = [textView positionFromPosition:beginning offset:range.location];
UITextPosition *end = [textView positionFromPosition:start offset:range.length];
UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];

// Insert that newline character *and* a bullet point
// at the point at which the user inputted just the
// newline character
[textView replaceRange:textRange withText:@"\n\u2022 "];

// Update the cursor position accordingly
NSRange cursor = NSMakeRange(range.location + @"\n\u2022 ".length, 0);
textView.selectedRange = cursor;

}
// Then return "NO, don't change the characters in range" since
// you've just done the work already
return NO;
}

// Else return yes
return YES;
}

How to make a bullet list with Swift?

use 2 labels inside a view for the columns. both labels being multulined

class Helper {

static func bulletedList(strings:[String], textColor:UIColor, font:UIFont, bulletColor:UIColor, bulletSize:BulletSize) -> NSAttributedString {
let textAttributesDictionary = [NSFontAttributeName : font, NSForegroundColorAttributeName:textColor]

let bulletAttributesDictionary = [NSFontAttributeName : font.withSize(bulletSize.rawValue), NSForegroundColorAttributeName:bulletColor]
let fullAttributedString = NSMutableAttributedString.init()

for string: String in strings
{
let bulletPoint: String = "\u{2022}"
let formattedString: String = "\(bulletPoint) \(string)\n"
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: formattedString)
let paragraphStyle = createParagraphAttribute()

attributedString.addAttributes([NSParagraphStyleAttributeName: paragraphStyle], range: NSMakeRange(0, attributedString.length))
attributedString.addAttributes(textAttributesDictionary, range: NSMakeRange(0, attributedString.length))

let string:NSString = NSString(string: formattedString)
let rangeForBullet:NSRange = string.range(of: bulletPoint)

attributedString.addAttributes(bulletAttributesDictionary, range: rangeForBullet)
fullAttributedString.append(attributedString)
}
return fullAttributedString
}

static func createParagraphAttribute() -> NSParagraphStyle {

var paragraphStyle: NSMutableParagraphStyle
paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: 15, options: NSDictionary() as! [String : AnyObject])]
paragraphStyle.defaultTabInterval = 15
paragraphStyle.firstLineHeadIndent = 0
paragraphStyle.lineSpacing = 3
paragraphStyle.headIndent = 10
return paragraphStyle
}
}

and simply use Helper.bulletedList to create your bulletted list as Attributed text for the label

iphone bullet point list

The "bullet" character is at Unicode code point U+2022. You can use it in a string with @"\u2022" or [NSString stringWithFormat:@"%C", 0x2022].

The "line feed" character is at Unicode code point U+000A, and is used as UIKit's newline character. You can use it in a string with @"\n".

For example, if you had an array of strings, you could make a bulleted list with something like this:

NSArray * items = ...;
NSMutableString * bulletList = [NSMutableString stringWithCapacity:items.count*30];
for (NSString * s in items)
{
[bulletList appendFormat:@"\u2022 %@\n", s];
}
textView.text = bulletList;

It won't indent lines like a "proper" word processor. "Bad things" will happen if your list items include newline characters (but what did you expect?).

(Apple doesn't guarantee that "\uXXXX" escapes work in NSString literals, but in practice they do if you use Apple's compiler.)

NSAttributedString inserting a bullet point?

The easy bit: [mutableAttributedString insertAttributedString: @"•\t" atIndex:0];

The hard bit. Something along the following lines. (This is extracted from a bigger project, but it may give you a decent start.)

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"•\texample bullet fill out the text to check what happens on the second line and make sure it is lining up OK"];

CTTextAlignment alignment = kCTLeftTextAlignment;
CGFloat paragraphSpacing = 0.0;
CGFloat paragraphSpacingBefore = 0.0;
CGFloat firstLineHeadIndent = 15.0;
CGFloat headIndent = 30.0;

CGFloat firstTabStop = 15.0; // width of your indent
CGFloat lineSpacing = 0.45;

CTTextTabRef tabArray[] = { CTTextTabCreate(0, firstTabStop, NULL) };

CFArrayRef tabStops = CFArrayCreate( kCFAllocatorDefault, (const void**) tabArray, 1, &kCFTypeArrayCallBacks );
CFRelease(tabArray[0]);

CTParagraphStyleSetting altSettings[] =
{
{ kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &lineSpacing},
{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &alignment},
{ kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(CGFloat), &firstLineHeadIndent},
{ kCTParagraphStyleSpecifierHeadIndent, sizeof(CGFloat), &headIndent},
{ kCTParagraphStyleSpecifierTabStops, sizeof(CFArrayRef), &tabStops},
{ kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), ¶graphSpacing},
{ kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), ¶graphSpacingBefore}
};

CTParagraphStyleRef style;
style = CTParagraphStyleCreate( altSettings, sizeof(altSettings) / sizeof(CTParagraphStyleSetting) );

if ( style == NULL )
{
NSLog(@"*** Unable To Create CTParagraphStyle in apply paragraph formatting" );
return;
}

[string addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:(NSObject*)style,(NSString*) kCTParagraphStyleAttributeName, nil] range:NSMakeRange(0,[string length])];

CFRelease(tabStops);
CFRelease(style);

You need to include the CoreText framework and then import CoreText/CoreText.h



Related Topics



Leave a reply



Submit