How to get a subString prior iOS 10? - string

In swift,I can use subSting() method to get a subString like this:
let sessionId = "this is a test"
let index = sessionId.index(sessionId.startIndex, offsetBy: 2)
let prefix = sessionId.substring(to: index)
But,today I found the method'availability is iOS 10 and later.How to handle this problem for you guys?
Availability iOS (10.0 and later), macOS (10.12 and later), tvOS (10.0 and later), watchOS (3.0 and later)

Related

does dropzone-amd-module.js support IE 11?

Per the dropzone site version 5.0 should support IE10+. I am using dropzone-amd-module.js ver 5.5 in a Durandal app and IE11 does not seem to support Symbol.iterator. Since I need to support IE11 at least for a little while I'm looking for options. Does any know what the latest version of dropzone-amd-module.js is that will run in IE11?
The fix was to remove any reference to Symbol.iterator wherever it occurs (a few dozen places) from dropzone-amd-module.js like this:
replace this
for (var _iterator2 = this.element.getElementsByTagName("div"), _isArray2 =
Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 :
_iterator2[Symbol.iterator]();;) {
with this
for (var _iterator2 = this.element.getElementsByTagName("div"), _isArray2 = true, _i2
= 0; ;) {

How to make a part of string bold in a in a UI label

I want to make bold some part of a big string. I am using Xamarin iOS and getting a big string from API. I need to make some part of the string as bold I tried <b>some text </b> this will not work for mono touch. What is the best option to make a string bold in run time in xamrin. iOS or in winforms applications?
Use AttributedString to apply attributes to different parts of the string. See example below that will only make the first half of the string bold
var BoldString = new NSMutableAttributedString (original_string);
var BoldTextAttributes = new UIStringAttributes {
Font = UIFont.FromName ("Helvetica-Bold", 20f)
};
BoldString.SetAttributes (BoldTextAttributes.Dictionary, new NSRange (0, OutOfStock.Length / 2)); // this range will apply the attribute only to the first half of the string
MyLabel.AttributedText = BoldString; // Assign the attributed text to your label

swift playground UITextField spawns keyboard that is too big

Swift in playground on Mac OS. When the user clicks in a UItextfield, a keyboard spawns but it is very large compared to the view and only the first few keys are available.
minimal example:
import UIKit
import PlaygroundSupport
class TesterViewController : UIViewController {
var testTextField : UITextField!
override func loadView() {
let view = UIView()
view.backgroundColor = .white
testTextField = UITextField()
testTextField.borderStyle = .roundedRect
testTextField.text = ""
view.addSubview(testTextField)
testTextField.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
testTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),
testTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
])
self.view = view
}
}
PlaygroundPage.current.liveView = TesterViewController()
screenshot
I face the same issue. It seems as if Playground has a hard-coded screen size of 768x1024 (run UIScreen.main.bounds in the Playground) and shows the keyboard according to this size, independently of the live view's actual size.
The best workaround I came up with is to increase the size of the view controller so that it matches the keyboard:
let vc = TesterViewController()
vc.preferredContentSize = vc.view.frame.size // or a custom CGSize
PlaygroundPage.current.liveView = vc
Of course this makes the view larger than you might want it to be, so I only use this workaround when I really have to access the on-screen keyboard for testing.
System keyboard is presented according to the key window size and in the case of a playground simulator the size is 768x1024. It looks like it is a bug.
A solution which works:
Instead of passing a view controller one should pass a window with a custom size.
let window = UIWindow(frame: CGRect(x: 0,
y: 0,
width: 768,
height: 1024))
let viewController = MyViewController()
window.rootViewController = viewController
window.makeKeyAndVisible()
PlaygroundPage.current.liveView = window
Works in Xcode 11 and above (latest tested version Xcode 12.2)
Edited
I have checked the answer which suggests to use "vc.preferredContentSize" but it doesn't work for me. Tested in Xcode 12.2
let vc = TextFieldViewController()
vc.preferredContentSize = CGSize(width: 768,height: 1024)
PlaygroundPage.current.liveView = vc
In reply to Adobels (Seo 21 '19) ...
I am not sure why he proposed a different approach.
I modified dr_barlo's approach of Oct 22 '17 as follows (and it works in Xcode 11 too):
let vc = TextFieldViewController()
vc.preferredContentSize = CGSize.init(width: 768,height: 1024)
PlaygroundPage.current.liveView = vc
They all appear equivalent in results if not internal workings.

UITextView undo manager do not work with replacement attributed string (iOS 6)

iOS 6 has been updated to use UITextView for rich text editing (a UITextView now earns an attributedText property —which is stupidly non mutable—). Here is a question asked on iOS 6 Apple forum under NDA, that can be made public since iOS 6 is now public...
In a UITextView, I can undo any font change but cannot undo a replacement in a copy of the view's attributed string. When using this code...
- (void) replace: (NSAttributedString*) old with: (NSAttributedString*) new
{
1. [[myView.undoManager prepareWithInvocationTarget:self] replace:new with:old];
2. old=new;
}
... undoing is working well.
But if I add a line to get the result visible in my view, the undoManager do not fire the "replace:with:" method as it should...
- (void) replace: (NSAttributedString*) old with: (NSAttributedString*) new
{
1. [[myView.undoManager prepareWithInvocationTarget:self] replace:new with:old];
2. old=new;
3. myView.attributedText=[[NSAttributedString alloc] initWithAttributedString:old];
}
Any idea? I have the same problem with any of the replacement methods, using a range or not, for MutableAttributedString I tried to use on line "2"...
Umm, wow I really didn't expect this to work! I couldn't find a solution so I just started trying anything and everything...
- (void)applyAttributesToSelection:(NSDictionary*)attributes {
UITextView *textView = self.contentCell.textView;
NSRange selectedRange = textView.selectedRange;
UITextRange *selectedTextRange = textView.selectedTextRange;
NSAttributedString *selectedText = [textView.textStorage attributedSubstringFromRange:selectedRange];
[textView.undoManager beginUndoGrouping];
[textView replaceRange:selectedTextRange withText:selectedText.string];
[textView.textStorage addAttributes:attributes range:selectedRange];
[textView.undoManager endUndoGrouping];
[textView setTypingAttributes:attributes];
}
Mac Catalyst (iOS14)
UITextView has undoManager that will manage undo and redo for free without any additional code.
But replacing its attributedText will reset the undoManager (Updating text and its attributes in textStorage not work for me too). I found that undo and redo will works normally when formatting text without replacing attributedText but by standard edit actions (Right click on highlighting text > Font > Bold (Mac Catalyst)).
So to fix this :
You need to set the allowsEditingTextAttributes of UITextView to be true, this will make UITextView support undo and redo of attributedText.
self.textView.allowsEditingTextAttributes = true
If you want to change the text of attributedText, use replace(_:withText:) of UITextInput, or insertText(_:) and deleteBackward() of UIKeyInput that UITextView conforming to.
self.textView.replace(self.textView.selectedTextRange!, withText: "test")
If you want to change attributes of text, use updateTextAttributes(conversionHandler:) of UITextView instead.
self.textView.updateTextAttributes { _ in
let font = UIFont.boldSystemFont(ofSize: 17)
let attributes: [NSAttributedString.Key: Any] = [
.font: font,
]
return attributes
}
For changing text and its attributes in specific range, modify the selectedRange of UITextView.
To implement undo and redo buttons, check this answer : https://stackoverflow.com/a/50530040/8637708
I have tested with Mac Catalyst, it should work on iOS and iPadOS too.
The Undo Manager is reset after setting its 'text' or 'attributedText' property, this is why it does not work. Whether this behavior is a bug or by design I don't know.
However, you can use the UITextInput protocol method instead.
(void)replaceRange:(UITextRange *)range withText:(NSString *)text
This works.

How get fonts installed or supportd in mobile using getProperty - java me

Hope you all will be fine. Can any one tell me how can i get the fonts installed or supported in the mobile. And suppose urdu font supported by the mobile then i set a condition like this.
[code]
import java.lang.*;
String value;
String key = "microedition.font"; // not real need value it's just to show what i want
value = System.getProperty( key );
If (value == urdu){
txtArea2.getStyle.setFont(value);
} else {
System.out.println("Urdu not supported);
}
[/code]
is it possible to do something like this.
Thank you.
MIDP 2.x defines 3 faces of font with 3 sizes + 3 styles.
FACE_MONOSPACE
FACE_PROPORTIONAL
FACE_SYSTEM
SIZE_LARGE
SIZE_MEDIUM
SIZE_SMALL
STYLE_BOLD
STYLE_ITALIC
STYLE_UNDERLINED
You can choose font by using these values as like the below code:
Font f = Font.getFont(FACE_SYSTEM | SIZE_MEDIUM | STYLE_ITALIC);
From MIDP 3.0, you can assign font name with installed font or downloaded font. as like:
Font[] f = Font.getAvailableFonts(); // Get available fonts
Font a = Font.getFont("Andale Mono", STYLE_ITALIC, 10); // Get specific font
Unfortunately, there is no development tools for MIDP3 now.

Resources